import { isBrowser, supportsLocalStorage, debugLog } from './helpers' // Safe reference to localStorage for Node.js environments const getLocalStorage = (): Storage | null => { if (isBrowser() && supportsLocalStorage()) { return localStorage } return null } /** Storage interface */ export interface SupportedStorage { getItem(key: string): string | null setItem(key: string, value: string): void removeItem(key: string): void } /** Memory storage implementation for non-browser environments */ export class MemoryStorage implements SupportedStorage { private store: { [key: string]: string } = {} getItem(key: string): string | null { return this.store[key] || null } setItem(key: string, value: string): void { this.store[key] = value } removeItem(key: string): void { delete this.store[key] } clear(): void { this.store = {} } } /** Memory storage adapter instance */ export const memoryStorageAdapter = new MemoryStorage() /** Get appropriate storage implementation */ export function getStorage(): SupportedStorage { if (isBrowser() && supportsLocalStorage()) { return localStorage } return memoryStorageAdapter } /** Storage manager for session persistence */ export class StorageManager { private storage: SupportedStorage private storageKey: string private debug: boolean constructor(storage?: SupportedStorage, storageKey: string = 'aira.auth.token', debug: boolean = false) { this.storage = storage || getStorage() this.storageKey = storageKey this.debug = debug debugLog(this.debug, '[slauth:storage] StorageManager created', { storageKey: this.storageKey, storageType: this.storage.constructor.name, customStorage: !!storage, stackTrace: new Error().stack?.split('\n').slice(2, 4).join('\n') }) } /** Load session from storage */ async loadSession(): Promise { const localStorageRef = getLocalStorage() debugLog(this.debug, '[slauth:storage] getSession - checking storage', { storageKey: this.storageKey, storageType: this.storage.constructor.name, allKeys: localStorageRef && this.storage === localStorageRef ? Object.keys(localStorageRef) : 'N/A' }) const data = this.storage.getItem(this.storageKey) debugLog(this.debug, '[slauth:storage] getSession - data retrieved', { storageKey: this.storageKey, hasData: !!data, dataLength: data?.length, dataPreview: data ? `${data.substring(0, 50)}...` : null }) if (!data) return null const session = JSON.parse(data) debugLog(this.debug, '[slauth:storage] Session parsed', { hasAccessToken: !!session.access_token, hasExpiresAt: !!session.expires_at, expiresAt: session.expires_at }) return session } /** Save session to storage */ async saveSession(session: any): Promise { const data = JSON.stringify(session) debugLog(this.debug, '[slauth:storage] saveSession - before setItem', { storageKey: this.storageKey, hasAccessToken: !!session.access_token, tokenPreview: session.access_token ? `${session.access_token.substring(0, 20)}...` : null, dataLength: data.length, storageType: this.storage.constructor.name }) this.storage.setItem(this.storageKey, data) const verification = this.storage.getItem(this.storageKey) debugLog(this.debug, '[slauth:storage] saveSession - after setItem verification', { storageKey: this.storageKey, saved: !!verification, dataMatch: verification === data, verificationLength: verification?.length }) } /** Remove session from storage */ async removeSession(): Promise { try { this.storage.removeItem(this.storageKey) } catch (error) { console.warn('[slauth] Failed to remove session from storage:', error) } } /** Get user from storage */ async getUser(): Promise { try { const userKey = `${this.storageKey}-user` const data = this.storage.getItem(userKey) if (!data) return null return JSON.parse(data) } catch (error) { console.warn('[slauth] Failed to get user from storage:', error) return null } } /** Save user to storage */ async saveUser(user: any): Promise { try { const userKey = `${this.storageKey}-user` const data = JSON.stringify(user) this.storage.setItem(userKey, data) } catch (error) { console.warn('[slauth] Failed to save user to storage:', error) } } /** Remove user from storage */ async removeUser(): Promise { try { const userKey = `${this.storageKey}-user` this.storage.removeItem(userKey) } catch (error) { console.warn('[slauth] Failed to remove user from storage:', error) } } /** Clear all auth data from storage */ async clearAll(): Promise { await this.removeSession() await this.removeUser() } /** Check if storage is available */ isAvailable(): boolean { try { const testKey = '__aira_auth_test__' this.storage.setItem(testKey, 'test') this.storage.removeItem(testKey) return true } catch { return false } } }