Skip to content

Commit ac92d16

Browse files
committed
Fix issue with new module setup in SSR
1 parent c45d494 commit ac92d16

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

module/src/auth.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ let tokens:
3838
| null // Initialized but not logged in
3939
| undefined; // Not initialized
4040

41-
// Synchronously load & parse the latest token value we have, if any
42-
try {
43-
// ! because actually parse(null) -> null, so it's ok
44-
tokens = JSON.parse(localStorage.getItem('tokens')!);
45-
} catch (e) {
46-
tokens = null;
47-
console.log('Invalid token', localStorage.getItem('tokens'), e);
41+
if (typeof localStorage !== 'undefined') { // Skip this in SSR or other non-persistent envs
42+
// Synchronously load & parse the latest token value we have, if any
43+
try {
44+
// ! because actually parse(null) -> null, so it's ok
45+
tokens = JSON.parse(localStorage.getItem('tokens')!);
46+
} catch (e) {
47+
tokens = null;
48+
console.log('Invalid token', localStorage.getItem('tokens'), e);
49+
}
4850
}
4951

5052
function setTokens(newTokens: typeof tokens) {

0 commit comments

Comments
 (0)