TypeError: Cannot read property 'split' of null
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.
Can you confirm that you have a properly formatted JWT in storage?
@guillaumeod are you using Ionic? Seems like a variation on/duplicate of https://github.com/auth0/angular2-jwt/issues/269
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
What do you get if you roll back to 0.1.27?
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 ?
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.
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.
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
When you receive the token, set in localstorage with equal key of configuration. Only after you must calling the decoder of token.
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.
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! 🙇♂️