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

@@ -1,7 +1,7 @@
'use client';
import { useState, useEffect, Suspense } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import { useSearchParams } from 'next/navigation';
import Link from 'next/link';
import { useAuth } from '@/lib/auth';
@@ -20,7 +20,6 @@ function MessageHandler({ setMessage }: { setMessage: (message: { type: string,
}
export default function LoginPage() {
const router = useRouter();
const searchParams = useSearchParams();
const { signIn, signInWithGoogle, user } = useAuth();
@@ -43,13 +42,21 @@ export default function LoginPage() {
// 如果用户已登录,重定向到原始页面或首页
useEffect(() => {
if (user) {
if (redirectUrl) {
router.push(redirectUrl);
} else {
router.push('/');
}
console.log('用户已登录,准备重定向', { redirectUrl });
// 添加短暂延时确保状态更新完成
setTimeout(() => {
if (redirectUrl) {
// 使用硬重定向替代router.push
console.log('重定向到原始URL:', redirectUrl);
window.location.href = redirectUrl;
} else {
console.log('重定向到首页');
window.location.href = '/';
}
}, 100);
}
}, [user, router, redirectUrl]);
}, [user, redirectUrl]);
const handleEmailSignIn = async (e: React.FormEvent) => {
e.preventDefault();