feat: Implement auth logging and session verification optimizations

This commit is contained in:
gpt-engineer-app[bot]
2025-10-12 19:02:59 +00:00
parent 3a58dcf62d
commit ffb0297854
3 changed files with 84 additions and 49 deletions

View File

@@ -1,3 +1,5 @@
import { authLog, authWarn, authError } from './authLogger';
/**
* Custom storage adapter for Supabase authentication that handles iframe localStorage restrictions.
* Falls back to sessionStorage or in-memory storage if localStorage is blocked.
@@ -15,7 +17,7 @@ class AuthStorage {
localStorage.removeItem('__supabase_test__');
this.storage = localStorage;
this.storageType = 'localStorage';
console.log('[AuthStorage] Using localStorage ✓');
authLog('[AuthStorage] Using localStorage ✓');
} catch {
// Try sessionStorage as fallback
try {
@@ -23,12 +25,12 @@ class AuthStorage {
sessionStorage.removeItem('__supabase_test__');
this.storage = sessionStorage;
this.storageType = 'sessionStorage';
console.warn('[AuthStorage] localStorage blocked, using sessionStorage ⚠️');
authWarn('[AuthStorage] localStorage blocked, using sessionStorage ⚠️');
} catch {
// Use in-memory storage as last resort
this.storageType = 'memory';
console.error('[AuthStorage] Both localStorage and sessionStorage blocked, using in-memory storage ⛔');
console.error('[AuthStorage] Sessions will NOT persist across page reloads!');
authError('[AuthStorage] Both localStorage and sessionStorage blocked, using in-memory storage ⛔');
authError('[AuthStorage] Sessions will NOT persist across page reloads!');
// Attempt to recover session from URL
this.attemptSessionRecoveryFromURL();
@@ -51,7 +53,7 @@ class AuthStorage {
const refreshToken = urlParams.get('refresh_token');
if (accessToken && refreshToken) {
console.log('[AuthStorage] Recovering session from URL parameters');
authLog('[AuthStorage] Recovering session from URL parameters');
// Store in memory
this.memoryStorage.set('sb-auth-token', JSON.stringify({
access_token: accessToken,
@@ -63,23 +65,23 @@ class AuthStorage {
window.history.replaceState({}, document.title, window.location.pathname);
}
} catch (error) {
console.error('[AuthStorage] Failed to recover session from URL:', error);
authError('[AuthStorage] Failed to recover session from URL:', error);
}
}
private handleStorageChange(event: StorageEvent) {
// Sync auth state across tabs
if (event.key?.startsWith('sb-') && event.newValue) {
console.log('[AuthStorage] Syncing auth state across tabs');
authLog('[AuthStorage] Syncing auth state across tabs');
}
}
getItem(key: string): string | null {
console.log('[AuthStorage] Getting key:', key);
authLog('[AuthStorage] Getting key:', key);
try {
if (this.storage) {
const value = this.storage.getItem(key);
console.log('[AuthStorage] Retrieved from storage:', !!value);
authLog('[AuthStorage] Retrieved from storage:', !!value);
if (value) {
// Verify it's not expired
@@ -94,7 +96,7 @@ class AuthStorage {
: parsed.expires_at * 1000; // Convert from seconds to milliseconds
if (parsed.expires_at && expiryTime < Date.now()) {
console.warn('[AuthStorage] Token expired, removing', {
authWarn('[AuthStorage] Token expired, removing', {
expires_at: parsed.expires_at,
expiryTime: new Date(expiryTime),
now: new Date()
@@ -103,24 +105,24 @@ class AuthStorage {
return null;
}
console.log('[AuthStorage] Token valid, expires:', new Date(expiryTime));
authLog('[AuthStorage] Token valid, expires:', new Date(expiryTime));
} catch (e) {
console.warn('[AuthStorage] Could not parse token for expiry check:', e);
authWarn('[AuthStorage] Could not parse token for expiry check:', e);
}
}
}
return value;
}
console.log('[AuthStorage] Using memory storage');
authLog('[AuthStorage] Using memory storage');
return this.memoryStorage.get(key) || null;
} catch (error) {
console.error('[AuthStorage] Error reading from storage:', error);
authError('[AuthStorage] Error reading from storage:', error);
return this.memoryStorage.get(key) || null;
}
}
setItem(key: string, value: string): void {
console.log('[AuthStorage] Setting key:', key);
authLog('[AuthStorage] Setting key:', key);
try {
if (this.storage) {
this.storage.setItem(key, value);
@@ -128,7 +130,7 @@ class AuthStorage {
// Always keep in memory as backup
this.memoryStorage.set(key, value);
} catch (error) {
console.error('[AuthStorage] Error writing to storage:', error);
authError('[AuthStorage] Error writing to storage:', error);
// Fallback to memory only
this.memoryStorage.set(key, value);
}
@@ -141,7 +143,7 @@ class AuthStorage {
}
this.memoryStorage.delete(key);
} catch (error) {
console.error('[AuthStorage] Error removing from storage:', error);
authError('[AuthStorage] Error removing from storage:', error);
this.memoryStorage.delete(key);
}
}
@@ -159,7 +161,7 @@ class AuthStorage {
// Clear all auth-related storage (for force logout)
clearAll(): void {
console.log('[AuthStorage] Clearing all auth storage');
authLog('[AuthStorage] Clearing all auth storage');
try {
if (this.storage) {
// Get all keys from storage
@@ -173,16 +175,16 @@ class AuthStorage {
// Remove all Supabase auth keys
keys.forEach(key => {
console.log('[AuthStorage] Removing key:', key);
authLog('[AuthStorage] Removing key:', key);
this.storage!.removeItem(key);
});
}
// Clear memory storage
this.memoryStorage.clear();
console.log('[AuthStorage] ✓ All auth storage cleared');
authLog('[AuthStorage] ✓ All auth storage cleared');
} catch (error) {
console.error('[AuthStorage] Error clearing storage:', error);
authError('[AuthStorage] Error clearing storage:', error);
// Still clear memory storage as fallback
this.memoryStorage.clear();
}