Enhance authentication and debugging experience by adding detailed logging for cookie checks, session management, and user redirection. Update middleware to log authentication cookie status and user login state. Refactor login and debug pages to use hard redirects for improved reliability and include session data display. Implement custom cookie handling in Supabase client for better session management.

This commit is contained in:
2025-04-23 17:50:14 +08:00
parent 1b4e0bafc7
commit ebb1e77ecc
8 changed files with 209 additions and 29 deletions

View File

@@ -94,10 +94,19 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
return { error };
}
// 登录成功,设置会话和用户信息
console.log('登录成功,用户信息:', data.user?.email);
setSession(data.session);
setUser(data.user);
router.push('/analytics');
// 使用硬重定向代替router.push确保页面完全刷新
console.log('准备重定向到分析页面');
// 添加短暂延时确保状态已更新,然后重定向
setTimeout(() => {
window.location.href = '/analytics';
}, 100);
return {};
} catch (error) {
console.error('登录过程出错:', error);

View File

@@ -33,12 +33,47 @@ try {
console.error('JWT解码失败:', error);
}
// 创建自定义cookie处理逻辑
const customStorage = {
getItem: (key: string): string | null => {
if (typeof document === 'undefined') return null;
const cookie = document.cookie
.split(';')
.find((c) => c.trim().startsWith(`${key}=`));
return cookie ? cookie.split('=')[1] : null;
},
setItem: (key: string, value: string): void => {
if (typeof document === 'undefined') return;
// 获取当前主机和端口处理localhost上的不同端口情况
const host = typeof window !== 'undefined' ? window.location.hostname : '';
// 设置cookie对localhost使用通用domain
document.cookie = `${key}=${value}; path=/; max-age=${60 * 60 * 24 * 7}; samesite=lax; domain=${host}`;
console.log(`Cookie ${key} 已设置domain=${host}`);
},
removeItem: (key: string): void => {
if (typeof document === 'undefined') return;
// 获取当前主机和端口处理localhost上的不同端口情况
const host = typeof window !== 'undefined' ? window.location.hostname : '';
// 删除cookie对localhost使用通用domain
document.cookie = `${key}=; path=/; max-age=0; samesite=lax; domain=${host}`;
console.log(`Cookie ${key} 已移除`);
},
};
// 创建Supabase客户端
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
auth: {
persistSession: true,
autoRefreshToken: true,
detectSessionInUrl: true,
storageKey: 'sb-auth-token',
storage: customStorage
}
});