Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(common): prevent errors when accessing local storage in certain browser setups (#36003)

Charisf0ff6cee8bf2
diff --git a/packages/common/gotrue.ts b/packages/common/gotrue.ts
index 309dd3ab51d..414e3c219ff 100644
--- a/packages/common/gotrue.ts
+++ b/packages/common/gotrue.ts
@@ -9,17 +9,28 @@ export const AUTH_NAVIGATOR_LOCK_DISABLED_KEY =
process.env.NEXT_PUBLIC_AUTH_NAVIGATOR_LOCK_KEY ||
'supabase.dashboard.auth.navigatorLock.disabled'
+/**
+ * Catches errors thrown when accessing localStorage. Safari with certain
+ * security settings throws when localStorage is accessed.
+ */
+function safeGetLocalStorage(key: string) {
+ try {
+ return globalThis?.localStorage?.getItem(key)
+ } catch {
+ return null
+ }
+}
+
const debug =
- process.env.NEXT_PUBLIC_IS_PLATFORM === 'true' &&
- globalThis?.localStorage?.getItem(AUTH_DEBUG_KEY) === 'true'
+ process.env.NEXT_PUBLIC_IS_PLATFORM === 'true' && safeGetLocalStorage(AUTH_DEBUG_KEY) === 'true'
const persistedDebug =
process.env.NEXT_PUBLIC_IS_PLATFORM === 'true' &&
- globalThis?.localStorage?.getItem(AUTH_DEBUG_PERSISTED_KEY) === 'true'
+ safeGetLocalStorage(AUTH_DEBUG_PERSISTED_KEY) === 'true'
const shouldEnableNavigatorLock =
process.env.NEXT_PUBLIC_IS_PLATFORM === 'true' &&
- !(globalThis?.localStorage?.getItem(AUTH_NAVIGATOR_LOCK_DISABLED_KEY) === 'true')
+ !(safeGetLocalStorage(AUTH_NAVIGATOR_LOCK_DISABLED_KEY) === 'true')
const shouldDetectSessionInUrl = process.env.NEXT_PUBLIC_AUTH_DETECT_SESSION_IN_URL
? process.env.NEXT_PUBLIC_AUTH_DETECT_SESSION_IN_URL === 'true'