Skip to content

Commit e26b668

Browse files
committed
Merge pull request #49 from kuzzleio/kuz-345-sdk-add-login-method
bugfix: login doesn't store jwtToken / I've tested this PR and it works. Going to merge.
2 parents 03b7d75 + 38d942b commit e26b668

File tree

5 files changed

+73
-7
lines changed

5 files changed

+73
-7
lines changed

dist/kuzzle.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,34 @@ Kuzzle.prototype.connect = function () {
602602
return this;
603603
};
604604

605+
/**
606+
* Set the jwtToken used to query kuzzle
607+
* @param token
608+
* @returns {Kuzzle}
609+
*/
610+
Kuzzle.prototype.setJwtToken = function(token) {
611+
this.jwtToken = token;
612+
return this;
613+
};
614+
615+
/**
616+
* Get the jwtToken used by kuzzle
617+
* @returns {Kuzzle}
618+
*/
619+
Kuzzle.prototype.getJwtToken = function() {
620+
return this.jwtToken;
621+
};
605622

623+
/**
624+
* Send login request to kuzzle with credentials
625+
* If login success, store the jwtToken into kuzzle object
626+
*
627+
* @param strategy
628+
* @param credentials
629+
* @param expiresIn
630+
* @param cb
631+
* @returns {Kuzzle}
632+
*/
606633
Kuzzle.prototype.login = function (strategy, credentials, expiresIn, cb) {
607634
var
608635
self = this,
@@ -625,7 +652,7 @@ Kuzzle.prototype.login = function (strategy, credentials, expiresIn, cb) {
625652

626653
this.query({controller: 'auth', action: 'login'}, {body: request}, {}, function(error, response) {
627654
if (error === null) {
628-
self.jwtToken = response.jwt;
655+
self.setJwtToken(response.result.jwt);
629656
renewAllSubscriptions.call(self);
630657

631658
if (typeof cb === 'function') {
@@ -643,6 +670,12 @@ Kuzzle.prototype.login = function (strategy, credentials, expiresIn, cb) {
643670
return self;
644671
};
645672

673+
/**
674+
* Send logout request to kuzzle with jwtToken.
675+
*
676+
* @param cb
677+
* @returns {Kuzzle}
678+
*/
646679
Kuzzle.prototype.logout = function (cb) {
647680
var
648681
self = this,
@@ -655,7 +688,7 @@ Kuzzle.prototype.logout = function (cb) {
655688

656689
this.query({controller: 'auth', action: 'logout'}, request, {}, function(error) {
657690
if (error === null) {
658-
self.jwtToken = undefined;
691+
self.setJwtToken(undefined);
659692

660693
if (typeof cb === 'function') {
661694
cb(null, self);

dist/kuzzle.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/kuzzle.min.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/kuzzle.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,34 @@ Kuzzle.prototype.connect = function () {
327327
return this;
328328
};
329329

330+
/**
331+
* Set the jwtToken used to query kuzzle
332+
* @param token
333+
* @returns {Kuzzle}
334+
*/
335+
Kuzzle.prototype.setJwtToken = function(token) {
336+
this.jwtToken = token;
337+
return this;
338+
};
339+
340+
/**
341+
* Get the jwtToken used by kuzzle
342+
* @returns {Kuzzle}
343+
*/
344+
Kuzzle.prototype.getJwtToken = function() {
345+
return this.jwtToken;
346+
};
330347

348+
/**
349+
* Send login request to kuzzle with credentials
350+
* If login success, store the jwtToken into kuzzle object
351+
*
352+
* @param strategy
353+
* @param credentials
354+
* @param expiresIn
355+
* @param cb
356+
* @returns {Kuzzle}
357+
*/
331358
Kuzzle.prototype.login = function (strategy, credentials, expiresIn, cb) {
332359
var
333360
self = this,
@@ -350,7 +377,7 @@ Kuzzle.prototype.login = function (strategy, credentials, expiresIn, cb) {
350377

351378
this.query({controller: 'auth', action: 'login'}, {body: request}, {}, function(error, response) {
352379
if (error === null) {
353-
self.jwtToken = response.jwt;
380+
self.setJwtToken(response.result.jwt);
354381
renewAllSubscriptions.call(self);
355382

356383
if (typeof cb === 'function') {
@@ -368,6 +395,12 @@ Kuzzle.prototype.login = function (strategy, credentials, expiresIn, cb) {
368395
return self;
369396
};
370397

398+
/**
399+
* Send logout request to kuzzle with jwtToken.
400+
*
401+
* @param cb
402+
* @returns {Kuzzle}
403+
*/
371404
Kuzzle.prototype.logout = function (cb) {
372405
var
373406
self = this,
@@ -380,7 +413,7 @@ Kuzzle.prototype.logout = function (cb) {
380413

381414
this.query({controller: 'auth', action: 'logout'}, request, {}, function(error) {
382415
if (error === null) {
383-
self.jwtToken = undefined;
416+
self.setJwtToken(undefined);
384417

385418
if (typeof cb === 'function') {
386419
cb(null, self);

test/kuzzle/constructor.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ describe('Kuzzle constructor', () => {
708708
});
709709

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

714714
kuzzle.login('local', loginCredentials, '1h', function(error, k) {

0 commit comments

Comments
 (0)