Skip to content

Commit 145d5a0

Browse files
authored
fix failed localstorage access to not crash the application (#4037)
1 parent 9be4d86 commit 145d5a0

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

.changeset/soft-fans-carry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@blitzjs/auth": minor
3+
---
4+
5+
fix failed localStorage access to not crash the application

packages/blitz-auth/src/client/index.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ class PublicDataStore {
9090

9191
clear() {
9292
deleteCookie(COOKIE_PUBLIC_DATA_TOKEN())
93-
localStorage.removeItem(LOCALSTORAGE_PUBLIC_DATA_TOKEN())
93+
try {
94+
localStorage.removeItem(LOCALSTORAGE_PUBLIC_DATA_TOKEN())
95+
} catch (err) {
96+
console.error("LocalStorage is not available", err)
97+
}
9498
this.updateState(emptyPublicData)
9599
}
96100

@@ -105,12 +109,17 @@ class PublicDataStore {
105109
}
106110

107111
private getToken() {
108-
const cookieValue = readCookie(COOKIE_PUBLIC_DATA_TOKEN())
109-
if (cookieValue) {
110-
localStorage.setItem(LOCALSTORAGE_PUBLIC_DATA_TOKEN(), cookieValue)
111-
return cookieValue
112-
} else {
113-
return localStorage.getItem(LOCALSTORAGE_PUBLIC_DATA_TOKEN())
112+
try {
113+
const cookieValue = readCookie(COOKIE_PUBLIC_DATA_TOKEN())
114+
if (cookieValue) {
115+
localStorage.setItem(LOCALSTORAGE_PUBLIC_DATA_TOKEN(), cookieValue)
116+
return cookieValue
117+
} else {
118+
return localStorage.getItem(LOCALSTORAGE_PUBLIC_DATA_TOKEN())
119+
}
120+
} catch (err) {
121+
console.error("LocalStorage is not available", err)
122+
return undefined
114123
}
115124
}
116125
}

0 commit comments

Comments
 (0)