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:
23
app/page.tsx
23
app/page.tsx
@@ -1,31 +1,38 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useAuth } from '@/lib/auth';
|
||||
|
||||
export default function Home() {
|
||||
const router = useRouter();
|
||||
const { user, isLoading } = useAuth();
|
||||
|
||||
// 添加调试日志
|
||||
console.log('根页面状态:', { isLoading, userAuthenticated: !!user });
|
||||
|
||||
useEffect(() => {
|
||||
if (!isLoading) {
|
||||
console.log('准备从根页面重定向', { isLoggedIn: !!user });
|
||||
|
||||
// 使用硬重定向确保页面完全刷新
|
||||
if (user) {
|
||||
// 已登录用户重定向到分析页面
|
||||
router.push('/analytics');
|
||||
console.log('用户已登录,重定向到分析页面');
|
||||
window.location.href = '/analytics';
|
||||
} else {
|
||||
// 未登录用户重定向到登录页面
|
||||
router.push('/login');
|
||||
console.log('用户未登录,重定向到登录页面');
|
||||
window.location.href = '/login';
|
||||
}
|
||||
}
|
||||
}, [user, isLoading, router]);
|
||||
}, [isLoading, user]);
|
||||
|
||||
// 显示加载指示器
|
||||
// 显示加载指示器,并包含状态信息
|
||||
return (
|
||||
<div className="flex items-center justify-center min-h-screen">
|
||||
<div className="text-center">
|
||||
<div className="animate-spin rounded-full h-16 w-16 border-t-2 border-b-2 border-blue-500 mx-auto"></div>
|
||||
<p className="mt-4 text-lg text-gray-700">正在加载...</p>
|
||||
<p className="mt-2 text-sm text-gray-500">
|
||||
状态: {isLoading ? '检查登录中' : (user ? '已登录' : '未登录')}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user