question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'split' of undefined (crashes site on logout)

See original GitHub issue

Hi 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

4reactions
gethinoakescommented, Jul 31, 2018

Thanks @d-go, that is much cleaner. I look forward to when isTokenExpired returns false for null tokens 🙂

4reactions
d-gocommented, Jul 31, 2018

Well… it seems that actually the isTokenExpired() method doesn’t handle null tokens, however, I think you can simplify your solution:

isLoggedIn() {
    const token = this.jwtHelper.tokenGetter();
    if (!token) return false;

    return !this.jwtHelper.isTokenExpired();
}

But I agree that if the token is null, the method should treat the token as expired

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found