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

@@ -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
}
});