Skip to content

Commit 86926de

Browse files
author
Gilles Ballini
committed
Merge pull request #67 from kuzzleio/optional-login-credentials
Login credentials now optional
2 parents b9c3701 + a66ac97 commit 86926de

File tree

3 files changed

+138
-13
lines changed

3 files changed

+138
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "1.7.2",
3+
"version": "1.7.3",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <support@kuzzle.io>",
66
"repository": {

src/kuzzle.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -427,24 +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-
if (!cb && typeof expiresIn === 'function') {
438-
cb = expiresIn;
439-
expiresIn = null;
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+
}
448+
}
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];
440458
}
441459

442-
Object.keys(credentials).forEach(function (key) {
443-
request[key] = credentials[key];
444-
});
445-
446-
if (['number', 'string'].indexOf(typeof expiresIn) !== -1) {
447-
request.expiresIn = expiresIn;
460+
if (typeof credentials === 'object') {
461+
Object.keys(credentials).forEach(function (key) {
462+
request[key] = credentials[key];
463+
});
448464
}
449465

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

test/kuzzle/constructor.test.js

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

753+
it('should handle login with only one argument and without callback', function (done) {
754+
var
755+
kuzzle;
756+
757+
this.timeout(200);
758+
759+
kuzzle = new Kuzzle('nowhere', {
760+
connect: 'manual'
761+
});
762+
763+
kuzzle.query = function(queryArgs, query, options, cb) {
764+
done();
765+
};
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'};
773+
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');
818+
kuzzle.login('local', '1h', function() {
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() {
858+
done();
859+
});
860+
});
861+
753862
it('should send a failed loginAttempt event if logging in fails', function (done) {
754863
var
755864
kuzzle = new Kuzzle('nowhere', {connect: 'manual'}),

0 commit comments

Comments
 (0)