Skip to content

Commit

Permalink
credentials in Kuzzle.login optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jenow committed Mar 1, 2016
1 parent c421fce commit a66ac97
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 19 deletions.
43 changes: 27 additions & 16 deletions src/kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,29 +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
};

// don't need credentials in case of an OAUTH strategy
if (typeof credentials === 'number' || typeof credentials === 'string') {
cb = expiresIn;
expiresIn = credentials;
},
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 (!cb && typeof expiresIn === 'function') {
cb = expiresIn;
expiresIn = null;
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
95 changes: 92 additions & 3 deletions test/kuzzle/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ describe('Kuzzle constructor', () => {
});
});

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

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

kuzzle.query = function(queryArgs, query, options, cb) {
cb(null, {result: {url: 'redirect'}});
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() {
should(kuzzle.jwtToken).be.undefined();
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();
});
});
Expand Down

0 comments on commit a66ac97

Please sign in to comment.