In the App.js of our react application, created several years ago using create-react-app, we check to see if a user is already logged in via a useEffect() that checks localStorage for a prior authentication token. Sometimes, we run into errors. Directly posting sentry error seems like a lazy way to post on this issue, however it also seems like the clearest and most effective way to articulate the issue:
Because we are using the ternary operator, presumably the error is being thrown for window.localStorage.getItem('auth-token'), i.e window.localStorage seems to be null. My plan to fix is to simply also check that window.localStorage is not null:
let token = localStorage ? localStorage.getItem('auth-token') : (window.localStorage ? window.localStorage.getItem('auth-token') : null)
I think this solution will work okay, even if not the perfect solution. My main question is why is it that localStorage and window.localStorage are sometimes null - shouldn't these always have some value even if an empty string? Also, is there a more proper way to get auth-tokens from localStorage that I'm missing here? The double ternary operators, and using window.localStorage as the fallback for lcoalStorage, don't quite seem 100% proper.