'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 content that uses useSearchParams function LoginContent() { 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); // Get message from URL and redirect URL useEffect(() => { if (searchParams) { const messageParam = searchParams.get('message'); if (messageParam) { setMessage({ type: 'info', content: messageParam }); } const redirect = searchParams.get('redirect'); if (redirect) { setRedirectUrl(decodeURIComponent(redirect)); } } }, [searchParams]); // If user is logged in, redirect to original page or home page useEffect(() => { if (user) { console.log('User is logged in, preparing to redirect', { redirectUrl }); // Add a short delay to ensure state updates are complete setTimeout(() => { if (redirectUrl) { // Use hard redirect instead of router.push console.log('Redirecting to original URL:', redirectUrl); window.location.href = redirectUrl; } else { console.log('Redirecting to home page'); 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'); } // After successful login, redirect via 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 (

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

); } // Main component wrapped in Suspense export default function LoginPage() { return ( Loading...}> ); }