Angular2-jwt: Error when token is null at decodeToken (Cannot read property 'split' of null)

Created on 15 Aug 2017  ·  5Comments  ·  Source: auth0/angular2-jwt

Calling the method isTokenExpired of JwtHelperService I got the following error:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'split' of null
TypeError: Cannot read property 'split' of null
    at JwtHelperService.webpackJsonp.../../../../@auth0/angular-jwt/src/jwthelper.service.js.JwtHelperService.decodeToken (jwthelper.service.js:62)
    at JwtHelperService.webpackJsonp.../../../../@auth0/angular-jwt/src/jwthelper.service.js.JwtHelperService.getTokenExpirationDate (jwthelper.service.js:75)
    at JwtHelperService.webpackJsonp.../../../../@auth0/angular-jwt/src/jwthelper.service.js.JwtHelperService.isTokenExpired (jwthelper.service.js:85)

This error occurred because the token obtained in the tokenGetter is null.

One possible solution would be to verify that the token obtained by the tokenGetter is null before attempting to split.

Thanks,
Fabio.

stale

Most helpful comment

I've also ran into this when following the readme. There are currently 2 merge requests waiting that could fix this, would appreciate it if one of those could be accepted.

All 5 comments

Yeah, I bumped into this yesterday. Had to check the token wasn't null before passing it in. Agree this would be better dealt with in the isTokenExpired function.

Can you please expand where this null check takes place?

I've also ran into this when following the readme. There are currently 2 merge requests waiting that could fix this, would appreciate it if one of those could be accepted.

LarsVonQualen answer this here: https://github.com/auth0/angular2-jwt/issues/274.

a work around is to put the token in the sesionstorage first and then decoded, i do it like this:
public getDecodedToken(): string { const token: string = window.sessionStorage.getItem('msal.idtoken'); var decodedToken = this.jwt.decodeToken(token) return decodedToken; }
i use msal, but if you use other this you must pass de token string in the sessionstorage.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you have not received a response for our team (apologies for the delay) and this is still a blocker, please reply with additional information or just a ping. Thank you for your contribution! 🙇‍♂️

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sfabriece picture sfabriece  ·  4Comments

huineng picture huineng  ·  4Comments

nickraphael picture nickraphael  ·  3Comments

JaxonWright picture JaxonWright  ·  4Comments

wannabegeek picture wannabegeek  ·  3Comments