TypeError: Cannot read property 'split' of undefined (crashes site on logout)
See original GitHub issueHi guys, this issue only occurs when the user is logged out and completely crashes the app. I get a bunch of errors about not being able to read ‘split’ of undefined.
AppComponent_Host.ngfactory.js? [sm]:1 ERROR TypeError: Cannot read property 'split' of undefined
at JwtHelperService.push../node_modules/@auth0/angular-jwt/src/jwthelper.service.js.JwtHelperService.decodeToken (jwthelper.service.js:70)
at JwtHelperService.push../node_modules/@auth0/angular-jwt/src/jwthelper.service.js.JwtHelperService.getTokenExpirationDate (jwthelper.service.js:83)
at JwtHelperService.push../node_modules/@auth0/angular-jwt/src/jwthelper.service.js.JwtHelperService.isTokenExpired (jwthelper.service.js:96)
at AuthService.push../src/app/core/auth/auth.service.ts.AuthService.isLoggedIn (auth.service.ts:124)
As I said, this only happens when the user logs out but completely crashed the app so they can’t log back in.
Here’s how JWT is implemented, it’s been this way for a few months and nothing’s changed so I have no idea what is going on here.
app.module.ts
export function tokenGetter() {
const user = JSON.parse(localStorage.getItem('user'));
const userToken = user ? user.token : null;
return userToken;
}
...
JwtModule.forRoot({
config: {
tokenGetter: tokenGetter,
whitelistedDomains: ['localhost:4200']
}
}),
auth.service.ts
import { JwtHelperService } from '@auth0/angular-jwt';
constructor(
private http: HttpClient,
private jwtHelper: JwtHelperService
) {}
...
isLoggedIn() {
return this.jwtHelper.isTokenExpired() ? false : true;
}
The only way I can get back into the app to log back in is to comment out the return statement within the isLoggedIn function.
Any help would be very much appreciated, thanks.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:6
Top Results From Across the Web
TypeError: Cannot read property 'split' of undefined (crashes ...
Hi guys, this issue only occurs when the user is logged out and completely crashes the app. I get a bunch of errors...
Read more >How to Prevent cannot read property split of undefined
The "cannot read property 'split' of undefined" error occurs when trying to call split() method on a variable that stores an undefined value....
Read more >Crash: Cannot read property 'split' of undefined - Question
When I try to text edit a field the app crashes with the following error: “Cannot read property 'split' of undefined” somewhere later...
Read more >Cannot Read Property 'split' of Undefined - freeCodeCamp
it will throw the TypeError: Cannot read property 'split' of undefined error. The split method. When split is called on a string, it...
Read more >Changelog - Cypress Documentation
When a chromium based browser tab or process crashes, Cypress will no longer hang ... The Cannot read property "fireChangeEvent" of undefined error...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks @d-go, that is much cleaner. I look forward to when
isTokenExpired
returns false for null tokens 🙂Well… it seems that actually the
isTokenExpired()
method doesn’t handle null tokens, however, I think you can simplify your solution:But I agree that if the token is null, the method should treat the token as expired