diff --git a/packages/auth-client/src/db.ts b/packages/auth-client/src/db.ts index adc3f8f73..cc864a4b6 100644 --- a/packages/auth-client/src/db.ts +++ b/packages/auth-client/src/db.ts @@ -1,5 +1,5 @@ import { openDB, IDBPDatabase } from 'idb'; -import { KEY_STORAGE_DELEGATION, KEY_STORAGE_KEY } from './storage'; +import { isBrowser, KEY_STORAGE_DELEGATION, KEY_STORAGE_KEY } from './storage'; type Database = IDBPDatabase; type IDBValidKey = string | number | Date | BufferSource | IDBValidKey[]; @@ -12,7 +12,7 @@ const _openDbStore = async ( version: number, ) => { // Clear legacy stored delegations - if (localStorage && localStorage.getItem(KEY_STORAGE_DELEGATION)) { + if (isBrowser && localStorage?.getItem(KEY_STORAGE_DELEGATION)) { localStorage.removeItem(KEY_STORAGE_DELEGATION); localStorage.removeItem(KEY_STORAGE_KEY); } diff --git a/packages/auth-client/src/index.ts b/packages/auth-client/src/index.ts index 323bbfb3d..5758f8466 100644 --- a/packages/auth-client/src/index.ts +++ b/packages/auth-client/src/index.ts @@ -18,6 +18,7 @@ import { IdleManager, IdleManagerOptions } from './idleManager'; import { AuthClientStorage, IdbStorage, + isBrowser, KEY_STORAGE_DELEGATION, KEY_STORAGE_KEY, KEY_VECTOR, @@ -195,7 +196,7 @@ export class AuthClient { key = options.identity; } else { let maybeIdentityStorage = await storage.get(KEY_STORAGE_KEY); - if (!maybeIdentityStorage) { + if (!maybeIdentityStorage && isBrowser) { // Attempt to migrate from localstorage try { const fallbackLocalStorage = new LocalStorage(); diff --git a/packages/auth-client/src/storage.ts b/packages/auth-client/src/storage.ts index 2a43876a3..8bbfed55c 100644 --- a/packages/auth-client/src/storage.ts +++ b/packages/auth-client/src/storage.ts @@ -6,6 +6,8 @@ export const KEY_VECTOR = 'iv'; // Increment if any fields are modified export const DB_VERSION = 1; +export const isBrowser = typeof window !== 'undefined'; + /** * Interface for persisting user authentication data */