Angular2-jwt: No JWT present or has expired after upgrading to Angular4

Created on 10 Apr 2017  ·  16Comments  ·  Source: auth0/angular2-jwt

Hi, I am getting this error after updating from Angular 2 to 4 and I am using the latest angular2-jwt

AuthHttpError {__zone_symbol__error: Error: No JWT present or has expired
    at AuthHttpError.ZoneAwareError (http://localhost:3000/poly……}

I found this issue but even if I comment out the getter function I still get this error.

export function AuthFactory(http: Http, options: RequestOptions) {
    return new AuthHttp(new AuthConfig({
        // tokenName: 'id_token',
        // tokenGetter: (() => localStorage.getItem('id_token')),
        globalHeaders: [{ 'Content-Type': 'application/json' }],
    }), http, options);
};

Any idea why I am getting this error?

Most helpful comment

Thanks! For everyone else getting this error:

angular2-jwt 0.1.28 + Angular 2.x = working
angular2-jwt 0.1.28 + Angular 4.x = working
angular2-jwt 0.2.0 + Angular 4.x = working
angular2-jwt 0.2.1 + Angular 4.x = not working
angular2-jwt 0.2.2 + Angular 4.x = not working
angular2-jwt 0.2.3 + Angular 4.x = not working

All 16 comments

How do you fix this issue?

Got the same. How did you fixed this issue?

I downgraded the version and works fine

The Angular version, or angular2-jwt version?

angular2-jwt version

Thanks! For everyone else getting this error:

angular2-jwt 0.1.28 + Angular 2.x = working
angular2-jwt 0.1.28 + Angular 4.x = working
angular2-jwt 0.2.0 + Angular 4.x = working
angular2-jwt 0.2.1 + Angular 4.x = not working
angular2-jwt 0.2.2 + Angular 4.x = not working
angular2-jwt 0.2.3 + Angular 4.x = not working

Confirmed that angular2-jwt 0.2.0 works with Angular 4.x

when this is fixed ?

Is this fixed? I am facing same error. Error is : - Error: No JWT present or has expired

Same problem here, downgraded to 0.2.0 and it works, oh well..

Ran into the same issue. However, even with 0.2.0 I cannot get it to work again.
When setting noClientCheck to true in the config, it works.

export function authHttpServiceFactory(http: Http, options: RequestOptions) {
  return new AuthHttp(new AuthConfig({
    noClientCheck: true
  }), http, options)
}

Just check with some co-workers, This problem might be due to the fact that the token name changed from access_token to token and that you must update your app. Here is the commit where they changed the documentation: a6984d1

On the latest version this can easily be fixed with adding the following inside:

auth.module.ts

```
export function authHttpServiceFactory(http: Http, options: RequestOptions) {
return new AuthHttp(new AuthConfig({
tokenName: 'rawJWT',
tokenGetter: (() => localStorage.getItem('rawJWT')),
}), http, options);
}
````

Where "rawJWT" is the name the token is stored against inside local browser storage. To find this out, on chrome, go to inspect --> application --> local storage there you will find the name/key used to store the token inside localstorage.

you are getting this error because you have not set the token correctly for your AuthHttp. your service factory should be something like this

> export function authHttpServiceFactory(http: Http, options: RequestOptions,authService:AuthService) {
>   return new AuthHttp(new AuthConfig({
>     tokenName: 'token',
>       tokenGetter: (() => authService.token),
>       globalHeaders: [{'Content-Type':'application/json'}],
>   }), http, options);
> }

I think the best practice is not to get the token from localstorage. but get it from a global service

I found that the default token name changed on me between versions. I was getting this error because in my app I had the token to register as "sessionToken" while angular2-jwt was trying to find id_token. After changing this it fixed my issue.

AuthConfigConsts.DEFAULT_TOKEN_NAME = 'id_token';

Used @jjmurre 's trick with noClientCheck: true.
Not sure how secure it is though...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tekkudoc picture tekkudoc  ·  5Comments

leosvelperez picture leosvelperez  ·  5Comments

jaumard picture jaumard  ·  5Comments

UlyssesAlves picture UlyssesAlves  ·  5Comments

Eddman picture Eddman  ·  3Comments