-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add authenticated property on Kuzzle object #390
Changes from 6 commits
011309a
ea134e1
799a2f2
15e5801
3a65501
7011630
dad0cb9
c749274
0f2e49e
a1bc297
7e62b84
747aa08
404aaf1
61763c3
36eff29
205840e
3fe9fe9
28c869f
a800b25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -203,6 +203,14 @@ class Kuzzle extends KuzzleEventEmitter { | |
return this.protocol.sslConnection; | ||
} | ||
|
||
isLoggued () { | ||
if (!this.jwt) { | ||
return false; | ||
} | ||
|
||
return this.jwtExpiresAt > Date.now(); | ||
} | ||
|
||
/** | ||
* Emit an event to all registered listeners | ||
* An event cannot be emitted multiple times before a timeout has been reached. | ||
|
@@ -239,6 +247,7 @@ class Kuzzle extends KuzzleEventEmitter { | |
|
||
this.protocol.addListener('tokenExpired', () => { | ||
this.jwt = undefined; | ||
this.jwtExpiresAt = undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's just question from a JS noob, what is the difference between: this.jwtExpiresAt = undefined; and delete this.jwtExpiresAt; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With |
||
this.emit('tokenExpired'); | ||
}); | ||
|
||
|
@@ -280,10 +289,12 @@ class Kuzzle extends KuzzleEventEmitter { | |
// shouldn't obtain an error but let's invalidate the token anyway | ||
if (!res.valid) { | ||
this.jwt = undefined; | ||
this.jwtExpiresAt = undefined; | ||
} | ||
}) | ||
.catch(() => { | ||
this.jwt = undefined; | ||
this.jwtExpiresAt = undefined; | ||
}) | ||
.then(() => this.emit('reconnected')); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This falls short as soon as the
jwt
property is set with a custom token (i.e. saved from an earlier connection), which is a very, very common situation.For this to work, you must also update the
jwt
setter, decode the token's payload and retrieve its timeout value.