Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,14 @@ Kuzzle.prototype.getJwtToken = function() {
Kuzzle.prototype.login = function (strategy) {
var
self = this,
request = {
strategy: strategy
},
request = {},
credentials,
cb = null;

if (!strategy || typeof strategy !== 'string') {
throw new Error('Kuzzle.login: strategy required');
}

// Handle arguments (credentials, expiresIn, cb)
if (arguments[1]) {
if (typeof arguments[1] === 'object') {
Expand Down Expand Up @@ -516,7 +518,7 @@ Kuzzle.prototype.login = function (strategy) {
});
}

this.query({controller: 'auth', action: 'login'}, {body: request}, {queuable: false}, function(error, response) {
this.query({controller: 'auth', action: 'login'}, {body: request, strategy: strategy}, {queuable: false}, function(error, response) {
if (!error) {
if (response.result.jwt) {
self.setJwtToken(response.result.jwt);
Expand Down
14 changes: 12 additions & 2 deletions test/kuzzle/login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,29 @@ describe('Kuzzle Login', function () {
kuzzle.login('local', loginCredentials, '1h');
should(queryStub.firstCall).be.calledWith(
{controller: 'auth', action: 'login'},
{body: {strategy: 'local', username: 'foo', password: 'bar'}},
{strategy: 'local', body: {username: 'foo', password: 'bar'}},
{queuable: false}
);
should(queryStub.secondCall).be.calledWith(
{controller: 'auth', action: 'login'},
{body: {strategy: 'local', username: 'foo', password: 'bar', expiresIn: '1h'}},
{strategy: 'local', body: {username: 'foo', password: 'bar', expiresIn: '1h'}},
{queuable: false}
);
});
});


describe('#Error Login', function () {
it('should throw if called without strategy', function (done) {
should(function () {
kuzzle.login();
}).throw(Error);
should(function () {
kuzzle.login({username: 'foo', password: 'bar'}, '1h');
}).throw(Error);
done();
});

it('should send a failed loginAttempt event if logging in fails', function (done) {
sandbox.stub(kuzzle, 'query', function(queryArgs, query, options, cb) {
cb({message: 'foobar'});
Expand Down