Skip to content

Commit a66ac97

Browse files
committed
credentials in Kuzzle.login optional
1 parent c421fce commit a66ac97

File tree

2 files changed

+119
-19
lines changed

2 files changed

+119
-19
lines changed

src/kuzzle.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -427,29 +427,40 @@ Kuzzle.prototype.getJwtToken = function() {
427427
* @param cb
428428
* @returns {Kuzzle}
429429
*/
430-
Kuzzle.prototype.login = function (strategy, credentials, expiresIn, cb) {
430+
Kuzzle.prototype.login = function (strategy) {
431431
var
432432
self = this,
433433
request = {
434434
strategy: strategy
435-
};
436-
437-
// don't need credentials in case of an OAUTH strategy
438-
if (typeof credentials === 'number' || typeof credentials === 'string') {
439-
cb = expiresIn;
440-
expiresIn = credentials;
435+
},
436+
credentials,
437+
cb;
438+
439+
// Handle arguments (credentials, expiresIn, cb)
440+
if (arguments[1]) {
441+
if (typeof arguments[1] === 'object') {
442+
credentials = arguments[1];
443+
} else if (typeof arguments[1] === 'number' || typeof arguments[1] === 'string') {
444+
request.expiresIn = arguments[1];
445+
} else if (typeof arguments[1] === 'function') {
446+
cb = arguments[1];
447+
}
441448
}
442-
if (!cb && typeof expiresIn === 'function') {
443-
cb = expiresIn;
444-
expiresIn = null;
449+
if (arguments[2]) {
450+
if (typeof arguments[2] === 'number' || typeof arguments[2] === 'string') {
451+
request.expiresIn = arguments[2];
452+
} else if (typeof arguments[2] === 'function') {
453+
cb = arguments[2];
454+
}
455+
}
456+
if (arguments[3] && typeof arguments[3] === 'function') {
457+
cb = arguments[3];
445458
}
446459

447-
Object.keys(credentials).forEach(function (key) {
448-
request[key] = credentials[key];
449-
});
450-
451-
if (['number', 'string'].indexOf(typeof expiresIn) !== -1) {
452-
request.expiresIn = expiresIn;
460+
if (typeof credentials === 'object') {
461+
Object.keys(credentials).forEach(function (key) {
462+
request[key] = credentials[key];
463+
});
453464
}
454465

455466
this.query({controller: 'auth', action: 'login'}, {body: request}, {queuable: false}, function(error, response) {

test/kuzzle/constructor.test.js

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ describe('Kuzzle constructor', () => {
750750
});
751751
});
752752

753-
it('should get the url redirection from OAUTH', function (done) {
753+
it('should handle login with only one argument and without callback', function (done) {
754754
var
755755
kuzzle;
756756

@@ -761,11 +761,100 @@ describe('Kuzzle constructor', () => {
761761
});
762762

763763
kuzzle.query = function(queryArgs, query, options, cb) {
764-
cb(null, {result: {url: 'redirect'}});
764+
done();
765765
};
766+
kuzzle.login('local');
767+
});
768+
769+
it('should handle login with credentials and without callback', function (done) {
770+
var
771+
kuzzle,
772+
loginCredentials = {username: 'foo', password: 'bar'};
766773

774+
this.timeout(200);
775+
776+
kuzzle = new Kuzzle('nowhere', {
777+
connect: 'manual'
778+
});
779+
780+
kuzzle.query = function(queryArgs, query, options, cb) {
781+
done();
782+
};
783+
784+
kuzzle.login('local', loginCredentials);
785+
});
786+
787+
it('should handle login without credentials, with expiresIn and without callback', function (done) {
788+
var
789+
kuzzle;
790+
791+
this.timeout(200);
792+
793+
kuzzle = new Kuzzle('nowhere', {
794+
connect: 'manual'
795+
});
796+
797+
kuzzle.query = function(queryArgs, query, options, cb) {
798+
done();
799+
};
800+
801+
kuzzle.login('local', '1h');
802+
});
803+
804+
it('should handle login without credentials, without expiresIn and with callback', function (done) {
805+
var
806+
kuzzle;
807+
808+
this.timeout(200);
809+
810+
kuzzle = new Kuzzle('nowhere', {
811+
connect: 'manual'
812+
});
813+
814+
kuzzle.query = function(queryArgs, query, options, cb) {
815+
cb(null, {result: {jwt: 'test-toto'}});
816+
};
817+
console.log('## before');
767818
kuzzle.login('local', '1h', function() {
768-
should(kuzzle.jwtToken).be.undefined();
819+
done();
820+
});
821+
});
822+
823+
it('should handle login without credentials, without expiresIn and with callback', function (done) {
824+
var
825+
kuzzle;
826+
827+
this.timeout(200);
828+
829+
kuzzle = new Kuzzle('nowhere', {
830+
connect: 'manual'
831+
});
832+
833+
kuzzle.query = function(queryArgs, query, options, cb) {
834+
cb(null, {result: {jwt: 'test-toto'}});
835+
};
836+
837+
kuzzle.login('local', function() {
838+
done();
839+
});
840+
});
841+
842+
it('should handle login with credentials', function (done) {
843+
var
844+
kuzzle,
845+
loginCredentials = {username: 'foo', password: 'bar'};
846+
847+
this.timeout(200);
848+
849+
kuzzle = new Kuzzle('nowhere', {
850+
connect: 'manual'
851+
});
852+
853+
kuzzle.query = function(queryArgs, query, options, cb) {
854+
cb(null, {result: {jwt: 'test-toto'}});
855+
};
856+
857+
kuzzle.login('local', loginCredentials, function() {
769858
done();
770859
});
771860
});

0 commit comments

Comments
 (0)