fix: 完善会话过期逻辑

This commit is contained in:
hzm
2026-02-08 07:14:43 +08:00
parent 4d4b931e30
commit 7772de1acf

View File

@@ -2438,8 +2438,25 @@ export default function HomePage() {
if (session.expires_at && session.expires_at * 1000 <= Date.now()) { if (session.expires_at && session.expires_at * 1000 <= Date.now()) {
isLoggingOutRef.current = true; isLoggingOutRef.current = true;
await supabase.auth.signOut({ scope: 'local' }); await supabase.auth.signOut({ scope: 'local' });
try {
const storageKeys = Object.keys(localStorage);
storageKeys.forEach((key) => {
if (key === 'supabase.auth.token' || (key.startsWith('sb-') && key.endsWith('-auth-token'))) {
localStorage.removeItem(key);
}
});
} catch { }
try {
const sessionKeys = Object.keys(sessionStorage);
sessionKeys.forEach((key) => {
if (key === 'supabase.auth.token' || (key.startsWith('sb-') && key.endsWith('-auth-token'))) {
sessionStorage.removeItem(key);
}
});
} catch { }
clearAuthState(); clearAuthState();
setLoginError('会话已过期,请重新登录'); setLoginError('会话已过期,请重新登录');
showToast('会话已过期,请重新登录', 'error');
setLoginModalOpen(true); setLoginModalOpen(true);
return; return;
} }
@@ -2564,17 +2581,39 @@ export default function HomePage() {
try { try {
const { data: { session } } = await supabase.auth.getSession(); const { data: { session } } = await supabase.auth.getSession();
if (session) { if (session) {
const { error } = await supabase.auth.signOut(); const { error } = await supabase.auth.signOut({ scope: 'local' });
if (error && error.code !== 'session_not_found') { if (error && error.code !== 'session_not_found') {
throw error; throw error;
} }
} }
} catch (err) { } catch (err) {
showToast(err.message, 'error')
console.error('登出失败', err); console.error('登出失败', err);
} finally { } finally {
try { try {
await supabase.auth.signOut({ scope: 'local' }); await supabase.auth.signOut({ scope: 'local' });
} catch { } } catch { }
try {
const storageKeys = Object.keys(localStorage);
storageKeys.forEach((key) => {
if (key === 'supabase.auth.token' || (key.startsWith('sb-') && key.endsWith('-auth-token'))) {
localStorage.removeItem(key);
}
});
} catch { }
try {
const sessionKeys = Object.keys(sessionStorage);
sessionKeys.forEach((key) => {
if (key === 'supabase.auth.token' || (key.startsWith('sb-') && key.endsWith('-auth-token'))) {
sessionStorage.removeItem(key);
}
});
} catch { }
setLoginModalOpen(false);
setLoginError('');
setLoginSuccess('');
setLoginEmail('');
setLoginOtp('');
setUserMenuOpen(false); setUserMenuOpen(false);
setUser(null); setUser(null);
} }