'use client'; import { useState, useEffect, Suspense } from 'react'; import { useSearchParams } from 'next/navigation'; import Link from 'next/link'; import { useAuth } from '@/lib/auth'; // Separate component for message handling to isolate useSearchParams function MessageHandler({ setMessage }: { setMessage: (message: { type: string, content: string }) => void }) { const searchParams = useSearchParams(); useEffect(() => { const messageParam = searchParams.get('message'); if (messageParam) { setMessage({ type: 'info', content: messageParam }); } }, [searchParams, setMessage]); return null; } export default function LoginPage() { const searchParams = useSearchParams(); const { signIn, signInWithGoogle, user } = useAuth(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [isLoading, setIsLoading] = useState(false); const [message, setMessage] = useState({ type: '', content: '' }); const [redirectUrl, setRedirectUrl] = useState(null); // 获取重定向URL useEffect(() => { if (searchParams) { const redirect = searchParams.get('redirect'); if (redirect) { setRedirectUrl(decodeURIComponent(redirect)); } } }, [searchParams]); // 如果用户已登录,重定向到原始页面或首页 useEffect(() => { if (user) { console.log('用户已登录,准备重定向', { redirectUrl }); // 添加短暂延时确保状态更新完成 setTimeout(() => { if (redirectUrl) { // 使用硬重定向替代router.push console.log('重定向到原始URL:', redirectUrl); window.location.href = redirectUrl; } else { console.log('重定向到首页'); window.location.href = '/'; } }, 100); } }, [user, redirectUrl]); const handleEmailSignIn = async (e: React.FormEvent) => { e.preventDefault(); if (!email || !password) { setMessage({ type: 'error', content: 'Please enter both email and password' }); return; } try { setIsLoading(true); setMessage({ type: '', content: '' }); const { error } = await signIn(email, password); if (error) { throw new Error(error instanceof Error ? error.message : 'Unknown error'); } // 登录成功后,会通过 useEffect 重定向 } catch (error) { console.error('Login error:', error); setMessage({ type: 'error', content: error instanceof Error ? error.message : 'Failed to sign in' }); setIsLoading(false); } }; const handleGoogleSignIn = async () => { try { setIsLoading(true); setMessage({ type: '', content: '' }); const { error } = await signInWithGoogle(); if (error) { throw new Error(error instanceof Error ? error.message : 'Unknown error'); } // Google OAuth will redirect the user } catch (error) { console.error('Google login error:', error); setMessage({ type: 'error', content: error instanceof Error ? error.message : 'Failed to sign in with Google' }); setIsLoading(false); } }; return (
{/* Wrap the component using useSearchParams in Suspense */}

Login

Sign in to your account to access analytics

Welcome to ShortURL Analytics
{/* Message display */} {message.content && (
{message.type === 'error' ? ( Error: ) : ( Notice: )} {message.content}
)}
setEmail(e.target.value)} className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500" placeholder="your@email.com" disabled={isLoading} />
setPassword(e.target.value)} className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500" placeholder="••••••••" disabled={isLoading} />
Or continue with

Don't have an account?{' '} Register

); }