mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 14:11:13 -05:00
Fix auth token expiry and storage
This commit is contained in:
@@ -75,24 +75,43 @@ class AuthStorage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getItem(key: string): string | null {
|
getItem(key: string): string | null {
|
||||||
|
console.log('[AuthStorage] Getting key:', key);
|
||||||
try {
|
try {
|
||||||
if (this.storage) {
|
if (this.storage) {
|
||||||
const value = this.storage.getItem(key);
|
const value = this.storage.getItem(key);
|
||||||
|
console.log('[AuthStorage] Retrieved from storage:', !!value);
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
// Verify it's not expired
|
// Verify it's not expired
|
||||||
if (key.includes('auth-token')) {
|
if (key.includes('auth-token')) {
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(value);
|
const parsed = JSON.parse(value);
|
||||||
if (parsed.expires_at && parsed.expires_at < Date.now()) {
|
|
||||||
console.warn('[AuthStorage] Token expired, removing');
|
// Supabase stores expires_at in seconds, Date.now() is in milliseconds
|
||||||
|
// Check if expires_at is in seconds (< year 3000 in milliseconds)
|
||||||
|
const expiryTime = parsed.expires_at > 10000000000
|
||||||
|
? parsed.expires_at // Already in milliseconds
|
||||||
|
: parsed.expires_at * 1000; // Convert from seconds to milliseconds
|
||||||
|
|
||||||
|
if (parsed.expires_at && expiryTime < Date.now()) {
|
||||||
|
console.warn('[AuthStorage] Token expired, removing', {
|
||||||
|
expires_at: parsed.expires_at,
|
||||||
|
expiryTime: new Date(expiryTime),
|
||||||
|
now: new Date()
|
||||||
|
});
|
||||||
this.removeItem(key);
|
this.removeItem(key);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
} catch {}
|
|
||||||
|
console.log('[AuthStorage] Token valid, expires:', new Date(expiryTime));
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('[AuthStorage] Could not parse token for expiry check:', e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
console.log('[AuthStorage] Using memory storage');
|
||||||
return this.memoryStorage.get(key) || null;
|
return this.memoryStorage.get(key) || null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[AuthStorage] Error reading from storage:', error);
|
console.error('[AuthStorage] Error reading from storage:', error);
|
||||||
@@ -101,6 +120,7 @@ class AuthStorage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setItem(key: string, value: string): void {
|
setItem(key: string, value: string): void {
|
||||||
|
console.log('[AuthStorage] Setting key:', key);
|
||||||
try {
|
try {
|
||||||
if (this.storage) {
|
if (this.storage) {
|
||||||
this.storage.setItem(key, value);
|
this.storage.setItem(key, value);
|
||||||
|
|||||||
Reference in New Issue
Block a user