Skip to content

Commit 616d8b9

Browse files
Ignore localStorage if it isn't available
localStorage is used to speed up different tabs/windows noticing that they are logged in/out. This checks to make sure it's available before we use it.
1 parent 3af78ad commit 616d8b9

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "git",
55
"url": "https://github.com/PropelAuth/javascript"
66
},
7-
"version": "1.1.0",
7+
"version": "1.1.1",
88
"keywords": [
99
"auth",
1010
"user",

src/client.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AuthenticationInfo, fetchAuthenticationInfo, logout } from "./api"
2-
import { currentTimeSeconds, getLocalStorageNumber } from "./helpers"
2+
import { currentTimeSeconds, getLocalStorageNumber, hasLocalStorage } from "./helpers"
33

44
const LOGGED_IN_AT_KEY = "__PROPEL_AUTH_LOGGED_IN_AT"
55
const LOGGED_OUT_AT_KEY = "__PROPEL_AUTH_LOGGED_OUT_AT"
@@ -268,6 +268,12 @@ export function createClient(authOptions: IAuthOptions): IAuthClient {
268268
}
269269

270270
const onStorageChange = async function () {
271+
// If localStorage isn't available, nothing to do here.
272+
// This usually happens in frameworks that have some SSR components
273+
if (!hasLocalStorage()) {
274+
return
275+
}
276+
271277
const loggedOutAt = getLocalStorageNumber(LOGGED_OUT_AT_KEY)
272278
const loggedInAt = getLocalStorageNumber(LOGGED_IN_AT_KEY)
273279

src/helpers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ export function currentTimeSeconds() {
22
return Date.now() / 1000
33
}
44

5+
export function hasLocalStorage(): boolean {
6+
return typeof localStorage !== 'undefined'
7+
}
8+
59
export function getLocalStorageNumber(key: string): number | null {
10+
if (!hasLocalStorage()) {
11+
return null;
12+
}
13+
614
const value = localStorage.getItem(key)
715
if (!value) {
816
return null

0 commit comments

Comments
 (0)