Angular2-jwt: Cannot make http requests when token is not present.

Created on 21 Jul 2017  路  9Comments  路  Source: auth0/angular2-jwt

I am getting following error when there is no token available and want to request server using HttpClient service:

TypeError: Cannot read property 'split' of null

Angular v4.3.1
angular-jwt v1.0.0-beta.2

Most helpful comment

This is config. Maybe we can add on readme.

import { JwtModule } from '@auth0/angular-jwt';
import { HttpClientModule } from '@angular/common/http';

Get token

export function getToken() {
  return localStorage.getItem('currentUser');
}

Import Jwt Module

    JwtModule.forRoot({
      config: {
        throwNoTokenError: false,
        tokenGetter: getToken,
        whitelistedDomains: ['localhost:4567']
      }
    }),

All 9 comments

@lekhnath token decode has problem.

@lekhnath which version of angular2-jwt are you using?

@chenkie I've mentioned both angular and angular-jwt version above.

having same issue.
decodeToken() should first check if there is token before decoding it (split, etc).

Or at least in the interceptor

same issue...

Fixed in 1.0.0-beta.3. There's also a new config value, throwNoTokenError which will optionally throw and error if no token exists. Defaults to false.

We might also look at using an upstream interceptor to handle stuff like this. cc @escardin

@chenkie would you update also readme? Seems like this line no longer work.

JwtModule.forRoot({
      config: {
        tokenGetter: () => {
          return localStorage.getItem('access_token');
        },
        whitelistedDomains: ['localhost:3001']
      }
    })

providing throwNoTokenError: false option to module configuration fixed the issue in 1.0.0-beta.3.
Thanks.

This is config. Maybe we can add on readme.

import { JwtModule } from '@auth0/angular-jwt';
import { HttpClientModule } from '@angular/common/http';

Get token

export function getToken() {
  return localStorage.getItem('currentUser');
}

Import Jwt Module

    JwtModule.forRoot({
      config: {
        throwNoTokenError: false,
        tokenGetter: getToken,
        whitelistedDomains: ['localhost:4567']
      }
    }),
Was this page helpful?
0 / 5 - 0 ratings

Related issues

wannabegeek picture wannabegeek  路  3Comments

huineng picture huineng  路  4Comments

guillaume-skwid picture guillaume-skwid  路  5Comments

Tirka picture Tirka  路  3Comments

leosvelperez picture leosvelperez  路  5Comments