Skip to content

Commit bc681a0

Browse files
Merge pull request #238 from kuzzleio/39-login-expiresIn
Login: move "expiresIn" option from request.body to request root attribute
2 parents 1d5f2df + 94c432b commit bc681a0

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

src/Kuzzle.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,10 @@ Kuzzle.prototype.getJwtToken = function() {
492492
Kuzzle.prototype.login = function (strategy) {
493493
var
494494
self = this,
495-
request = {},
496-
credentials,
495+
request = {
496+
body: {},
497+
strategy: strategy
498+
},
497499
cb = null;
498500

499501
if (!strategy || typeof strategy !== 'string') {
@@ -503,7 +505,7 @@ Kuzzle.prototype.login = function (strategy) {
503505
// Handle arguments (credentials, expiresIn, cb)
504506
if (arguments[1]) {
505507
if (typeof arguments[1] === 'object') {
506-
credentials = arguments[1];
508+
request.body = arguments[1];
507509
} else if (typeof arguments[1] === 'number' || typeof arguments[1] === 'string') {
508510
request.expiresIn = arguments[1];
509511
} else if (typeof arguments[1] === 'function') {
@@ -521,13 +523,7 @@ Kuzzle.prototype.login = function (strategy) {
521523
cb = arguments[3];
522524
}
523525

524-
if (typeof credentials === 'object') {
525-
Object.keys(credentials).forEach(function (key) {
526-
request[key] = credentials[key];
527-
});
528-
}
529-
530-
this.query({controller: 'auth', action: 'login'}, {body: request, strategy: strategy}, {queuable: false}, function(error, response) {
526+
this.query({controller: 'auth', action: 'login'}, request, {queuable: false}, function(error, response) {
531527
if (!error) {
532528
if (response.result.jwt) {
533529
self.setJwtToken(response.result.jwt);

test/kuzzle/login.test.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,47 @@ describe('Kuzzle Login', function () {
2929
it('should handle login with only the strategy', function (done) {
3030
kuzzle.login('local', function() {
3131
should(queryStub).be.calledOnce();
32+
should(queryStub.firstCall).be.calledWith(
33+
{controller: 'auth', action: 'login'},
34+
{body: {}, strategy: 'local'},
35+
{queuable: false}
36+
);
3237
done();
3338
});
3439
});
3540

3641
it('should handle login with credentials', function (done) {
3742
kuzzle.login('local', loginCredentials, function() {
3843
should(queryStub).be.calledOnce();
44+
should(queryStub.firstCall).be.calledWith(
45+
{controller: 'auth', action: 'login'},
46+
{body: {username: 'foo', password: 'bar'}, strategy: 'local'},
47+
{queuable: false}
48+
);
3949
done();
4050
});
4151
});
4252

4353
it('should handle login without credentials and with expiresIn', function (done) {
4454
kuzzle.login('local', '1h', function() {
4555
should(queryStub).be.calledOnce();
56+
should(queryStub.firstCall).be.calledWith(
57+
{controller: 'auth', action: 'login'},
58+
{body: {}, strategy: 'local', expiresIn: '1h'},
59+
{queuable: false}
60+
);
4661
done();
4762
});
4863
});
4964

5065
it('should have the token in login callback', function (done) {
5166
kuzzle.login('local', loginCredentials, '1h', function() {
5267
should(kuzzle.jwtToken).be.exactly('test-toto');
68+
should(queryStub.firstCall).be.calledWith(
69+
{controller: 'auth', action: 'login'},
70+
{body: {username: 'foo', password: 'bar'}, strategy: 'local', expiresIn: '1h'},
71+
{queuable: false}
72+
);
5373
done();
5474
});
5575
});
@@ -65,29 +85,39 @@ describe('Kuzzle Login', function () {
6585
it('should handle login with only the strategy', function () {
6686
kuzzle.login('local');
6787
should(queryStub).be.calledOnce();
88+
should(queryStub.firstCall).be.calledWith(
89+
{controller: 'auth', action: 'login'},
90+
{body: {}, strategy: 'local'},
91+
{queuable: false}
92+
);
6893
});
6994

7095
it('should handle login with credentials', function () {
7196
kuzzle.login('local', loginCredentials);
7297
should(queryStub).be.calledOnce();
98+
should(queryStub.firstCall).be.calledWith(
99+
{controller: 'auth', action: 'login'},
100+
{body: {username: 'foo', password: 'bar'}, strategy: 'local'},
101+
{queuable: false}
102+
);
73103
});
74104

75105
it('should handle login without credentials and with expiresIn', function () {
76106
kuzzle.login('local', '1h');
77107
should(queryStub).be.calledOnce();
108+
should(queryStub.firstCall).be.calledWith(
109+
{controller: 'auth', action: 'login'},
110+
{body: {}, strategy: 'local', expiresIn: '1h'},
111+
{queuable: false}
112+
);
78113
});
79114

80115
it('should handle optional arguments correctly', function () {
81-
kuzzle.login('local', loginCredentials);
82116
kuzzle.login('local', loginCredentials, '1h');
117+
should(queryStub).be.calledOnce();
83118
should(queryStub.firstCall).be.calledWith(
84119
{controller: 'auth', action: 'login'},
85-
{strategy: 'local', body: {username: 'foo', password: 'bar'}},
86-
{queuable: false}
87-
);
88-
should(queryStub.secondCall).be.calledWith(
89-
{controller: 'auth', action: 'login'},
90-
{strategy: 'local', body: {username: 'foo', password: 'bar', expiresIn: '1h'}},
120+
{body: {username: 'foo', password: 'bar'}, strategy: 'local', expiresIn: '1h'},
91121
{queuable: false}
92122
);
93123
});

0 commit comments

Comments
 (0)