Skip to content

Commit

Permalink
Merge pull request #67 from kuzzleio/optional-login-credentials
Browse files Browse the repository at this point in the history
Login credentials now optional
  • Loading branch information
Gilles Ballini committed Mar 1, 2016
2 parents b9c3701 + a66ac97 commit 86926de
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kuzzle-sdk",
"version": "1.7.2",
"version": "1.7.3",
"description": "Official Javascript SDK for Kuzzle",
"author": "The Kuzzle Team <support@kuzzle.io>",
"repository": {
Expand Down
40 changes: 28 additions & 12 deletions src/kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,24 +427,40 @@ Kuzzle.prototype.getJwtToken = function() {
* @param cb
* @returns {Kuzzle}
*/
Kuzzle.prototype.login = function (strategy, credentials, expiresIn, cb) {
Kuzzle.prototype.login = function (strategy) {
var
self = this,
request = {
strategy: strategy
};

if (!cb && typeof expiresIn === 'function') {
cb = expiresIn;
expiresIn = null;
},
credentials,
cb;

// Handle arguments (credentials, expiresIn, cb)
if (arguments[1]) {
if (typeof arguments[1] === 'object') {
credentials = arguments[1];
} else if (typeof arguments[1] === 'number' || typeof arguments[1] === 'string') {
request.expiresIn = arguments[1];
} else if (typeof arguments[1] === 'function') {
cb = arguments[1];
}
}
if (arguments[2]) {
if (typeof arguments[2] === 'number' || typeof arguments[2] === 'string') {
request.expiresIn = arguments[2];
} else if (typeof arguments[2] === 'function') {
cb = arguments[2];
}
}
if (arguments[3] && typeof arguments[3] === 'function') {
cb = arguments[3];
}

Object.keys(credentials).forEach(function (key) {
request[key] = credentials[key];
});

if (['number', 'string'].indexOf(typeof expiresIn) !== -1) {
request.expiresIn = expiresIn;
if (typeof credentials === 'object') {
Object.keys(credentials).forEach(function (key) {
request[key] = credentials[key];
});
}

this.query({controller: 'auth', action: 'login'}, {body: request}, {queuable: false}, function(error, response) {
Expand Down
109 changes: 109 additions & 0 deletions test/kuzzle/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,115 @@ describe('Kuzzle constructor', () => {
});
});

it('should handle login with only one argument and without callback', function (done) {
var
kuzzle;

this.timeout(200);

kuzzle = new Kuzzle('nowhere', {
connect: 'manual'
});

kuzzle.query = function(queryArgs, query, options, cb) {
done();
};
kuzzle.login('local');
});

it('should handle login with credentials and without callback', function (done) {
var
kuzzle,
loginCredentials = {username: 'foo', password: 'bar'};

this.timeout(200);

kuzzle = new Kuzzle('nowhere', {
connect: 'manual'
});

kuzzle.query = function(queryArgs, query, options, cb) {
done();
};

kuzzle.login('local', loginCredentials);
});

it('should handle login without credentials, with expiresIn and without callback', function (done) {
var
kuzzle;

this.timeout(200);

kuzzle = new Kuzzle('nowhere', {
connect: 'manual'
});

kuzzle.query = function(queryArgs, query, options, cb) {
done();
};

kuzzle.login('local', '1h');
});

it('should handle login without credentials, without expiresIn and with callback', function (done) {
var
kuzzle;

this.timeout(200);

kuzzle = new Kuzzle('nowhere', {
connect: 'manual'
});

kuzzle.query = function(queryArgs, query, options, cb) {
cb(null, {result: {jwt: 'test-toto'}});
};
console.log('## before');
kuzzle.login('local', '1h', function() {
done();
});
});

it('should handle login without credentials, without expiresIn and with callback', function (done) {
var
kuzzle;

this.timeout(200);

kuzzle = new Kuzzle('nowhere', {
connect: 'manual'
});

kuzzle.query = function(queryArgs, query, options, cb) {
cb(null, {result: {jwt: 'test-toto'}});
};

kuzzle.login('local', function() {
done();
});
});

it('should handle login with credentials', function (done) {
var
kuzzle,
loginCredentials = {username: 'foo', password: 'bar'};

this.timeout(200);

kuzzle = new Kuzzle('nowhere', {
connect: 'manual'
});

kuzzle.query = function(queryArgs, query, options, cb) {
cb(null, {result: {jwt: 'test-toto'}});
};

kuzzle.login('local', loginCredentials, function() {
done();
});
});

it('should send a failed loginAttempt event if logging in fails', function (done) {
var
kuzzle = new Kuzzle('nowhere', {connect: 'manual'}),
Expand Down

0 comments on commit 86926de

Please sign in to comment.