tokenNotExpired() always return false
Hi, I use your library without Ionic (native Angular 2) and all time tokenNotExpired() method return false.
When this code is exectued :
loggedIn() {
console.log(tokenNotExpired());
return tokenNotExpired();
}
constructor(private router: Router) {}
canActivate() {
console.log(this.loggedIn());
if(this.loggedIn()) {
return true;
} else {
this.router.navigate(['login']);
return false;
}
}
I use this code for angular routing canActivate: [AuthGuard].
But tokenNotExpired() always return false.
I don't understand why !
the token is valid, so isTokenExpired should return false. It's certainly confusing.
I update the post thx guy !
so return !isTokenExpired()
I think I found the solution, as I was fighting with a similar error: The error is induced by Commit a6984d1df9315cc52b22b9c57b8badc4b6d1f47 (update token name). With this commit, angular2-jwt assumes that the standard name of the token is token , not any longer access-token. The problem can be traced to the Quickstart of Auth0 for Angular 2 (https://auth0.com/docs/quickstart/spa/angular2/01-login), as it states (as of time of writing this):
// app/auth.service.ts
import { Injectable } from '@angular/core';
import { tokenNotExpired } from 'angular2-jwt';
// Avoid name not found warnings
declare var Auth0Lock: any;
@Injectable()
export class Auth {
// Configure Auth0
lock = new Auth0Lock('MyClientID', 'my.domain.dom', {});
constructor() {
// Add callback for lock `authenticated` event
this.lock.on("authenticated", (authResult) => {
localStorage.setItem('id_token', authResult.idToken);
});
}
public authenticated() {
// Check if there's an unexpired JWT
// This searches for an item in localStorage with key == 'id_token'
return tokenNotExpired();
}
}
So there are two possible fixes:
-Change in the constructor from localStorage.setItem('id_token', authResult.idToken)
to
localStorage.setItem('token', authResult.idToken)
-Use tokenNotExpired('id_token') instead of tokenNotExpired()
The third, impossible and stupid solution would be changing the default token name in JWT again.
Had the same issue. @qmsq's fix worked for me. Thank you.
A big thank you @qmsq!
The second solution looks better to me, since it's not very consistent to write localStorage.setItem('token', authResult.idToken) but this is a pure matter of preference.
Big up again, I was stuck on this for the last couple hours (grrr).
Yes, @qmsq's fix worked for me as well, thanks so much!
@qmsq made my day! Thank you!
same problem, the function is return from localStorage but is not using the factory options of tokenGetter
The implementation with google chrome debugger is:
function tokenNotExpired(tokenName, jwt) {
if (tokenName === void 0) { tokenName = AuthConfigConsts.DEFAULT_TOKEN_NAME; }
var token = jwt || localStorage.getItem(tokenName);// here is force to get from localStorage but not tokenGetter options of AuthConfig
var jwtHelper = new JwtHelper();
return token != null && !jwtHelper.isTokenExpired(token);
}
So another options is to pass the token in second parameters.
tokenNotExpired(null, sessionStorage.getItem("id_token"))
Thank...
Changing default token name was a tiny but breaking change in minor version update are unexpected. It happens... Looks like I need to constraint my lib versions to exact ones...
@qmsq return tokenNotExpired('id_token'); saved my day! ty
@qmsq thanks a lot man, you save my life.
I know this is closed, but can you update the README, as its confusing because the steps doesn't say that you now have to provide a parameter in return tokenNotExpired('id_token'). I'm following a tutorial using this and the guy didn't pass a parameter and it worked fine.
In case anyone is still a little confused I needed to do a little more detective work to understand the issue. Because I'm thinking wait isn't this supposed to be a self contained package?
The token used to be access-token and then it was changed to token
AuthConfigConsts.DEFAULT_TOKEN_NAME = 'token';
So I'm thinking how the #$ does changing it to id_token make it work? Why aren't we changing it to access-token.
I see in my auth.service.ts file it says id_token. So how did it ever work when the default token was access-token.
Turns out when looking at the history on April 11th the default token name was changed TWICE in one day. https://github.com/auth0/angular2-jwt/commits/a6984d1df9315cc52b22b9c57b8badc4b6d1f479/angular2-jwt.ts
id_token > access_token > token
So if like me you haven't touched your project since before April 11th then you'll change from id_token > token when you update the package.
I feel like tokenNotExpired('id_token') isn't the best solution, and changing the 'localstorage' property is better.
Alternative solution :
Modify your auth.service.ts file:
import { AuthConfigConsts } from 'angular2-jwt';
Search and replace all "id_token" to AuthConfigConsts.DEFAULT_TOKEN_NAME
@simeyla id_token and access_token are not the same, and they have changed it not so long ago. id_token is authentication (who you are) and access_token is authorization (it's sent as a Bearer token to the API). in the latest version the Auth0Lock by default returns only access_token, but not the id_token. I suppose you can change your auth.service.ts to check for access-token in authenticated() method or make Auth0Lock return id_token using options. What I did in auth.service.ts is:
var options = {
auth: {
params: {scope: 'openid email user_metadata app_metadata picture',
responseType: 'id_token token'},
}
};
....
lock = new Auth0Lock(myConfig.clientID, myConfig.domain, options);
and then:
this.lock.on('authenticated', (authResult) => {
localStorage.setItem('token', authResult.idToken);
localStorage.setItem('accessToken', authResult.accessToken);
hope it helps.
Thanks. So the double check in on April 11th was just a misunderstanding of this distinction. On Fri, May 19, 2017 at 21:15 Valerii Sosliuk [email protected] wrote:
@simeyla https://github.com/simeyla id_token and access_token are not the same, and they have changed it not so long ago. id_token is authentication (who you are) and access_token is authorization (it's sent as a Bearer token to API). in the latest version the Auth0Lock by default, returns only access_token, and not the id_token. I suppose you can change your auth.service.ts to check for access-token in authenticated method or make Auth0Lock return id_token using options. What I in auth.service.ts is:
var options = { auth: { params: {scope: 'openid email user_metadata app_metadata picture', responseType: 'id_token token'}, } }; .... lock = new Auth0Lock(myConfig.clientID, myConfig.domain, options);
and then:
this.lock.on('authenticated', (authResult) => {
localStorage.setItem('token', authResult.idToken); localStorage.setItem('accessToken', authResult.accessToken);hope it helps.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/auth0/angular2-jwt/issues/334#issuecomment-302849402, or mute the thread https://github.com/notifications/unsubscribe-auth/ADfKVqhj-0FnfYA9tqoIEYFKPlgMA2j4ks5r7mj9gaJpZM4M8mdi .
You can try
loggedIn() {
if (localStorage.getItem('token')) return tokenNotExpired()
}
we love you @qmsq
Thank you @qmsq !