angular2-jwt icon indicating copy to clipboard operation
angular2-jwt copied to clipboard

TypeError: Cannot read property 'split' of null

Open skwidy opened this issue 9 years ago • 11 comments

I get "TypeError: Cannot read property 'split' of null" using angular2-jwt when trying to decode the the token in my scheduler.

I fixed the problem like this but not very clean :) :

    JwtHelper.prototype.decodeToken = function (token) { 
        token = localStorage.getItem('access_token');
        var parts = token.split('.');
        if (parts.length !== 3) {
            throw new Error('JWT must have 3 parts');
        }
        var decoded = this.urlBase64Decode(parts[1]);
        if (!decoded) {
            throw new Error('Cannot decode the token');
        }
        return JSON.parse(decoded);
    };

Seems maybe something is broken in

        var token = this.config.tokenGetter();

?

Thanks!

EDIT: actually it doesn't fix it :/ It works, and then it doesn't ... I guess the problem is on my end ? It is behaving weirdly.

skwidy avatar Jan 17 '17 08:01 skwidy

Can you confirm that you have a properly formatted JWT in storage?

chenkie avatar Jan 17 '17 14:01 chenkie

@guillaumeod are you using Ionic? Seems like a variation on/duplicate of https://github.com/auth0/angular2-jwt/issues/269

escardin avatar Jan 17 '17 14:01 escardin

capture d ecran 2017-01-17 a 16 03 01

Yep, 2 dots & 3 parts :/

Setting token = localStorage.getItem('access_token'); in the .js seems to work but since it seems to be already coded this way ...

PS : it worked perfectly well 1-2 weeks ago when I implemented it, then one of my colleague updated his repo and got the error, so I tried and the same happened on my end although I'm pretty sure I changed nothing

skwidy avatar Jan 17 '17 15:01 skwidy

What do you get if you roll back to 0.1.27?

chenkie avatar Jan 17 '17 15:01 chenkie

Same thing Edit: adding localStorage.getItem also seems to fix it on 1.27.

So, am I supposed to override the token getter in the AuthConfig ?

skwidy avatar Jan 17 '17 15:01 skwidy

I'm getting this error when running Karma 1.4. I tried rolling back to version 0.1.27 and didn't work.

I'm using Angular Cli 1.0 and Angular 4.

ghost avatar Apr 14 '17 23:04 ghost

This error is very easy to reproduce if you use the JwtHelper class directly, and you do something like this.jwtHelper.isTokenExpired(localStorage.getItem('token')) where the token key is empty in localStorage thus returning null.

This issue though is handled in the exported tokenNotExpired method, but that method has a weird signature, if you want to supply the token yourself. tokenNotExpired(undefined, 'myJwt') is just not really pleasant 😄. But this is the correct way of checking expiration of your token.

LarsVonQualen avatar Apr 18 '17 07:04 LarsVonQualen

Just a follow up here

You just need to follow the advanced section and use the following code by declaring it as a module.

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

by setting token as access_token it resolved my problems ! See http://stackoverflow.com/questions/42475903/angular-2-factory-provider if you have trouble implementing it

cheers

guillaume-skwid avatar May 16 '17 09:05 guillaume-skwid

When you receive the token, set in localstorage with equal key of configuration. Only after you must calling the decoder of token.

edmarcoslins avatar Feb 17 '19 02:02 edmarcoslins

So this one is still not working and have to live with the workaround or workaround is the solution? If so please close this otherwise confusing.

sk29110 avatar Feb 20 '19 16:02 sk29110

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! 🙇‍♂️

stale[bot] avatar Oct 26 '19 03:10 stale[bot]