"use client" import { useState } from "react" import { signIn } from "next-auth/react" export default function LoginPage() { const [email, setEmail] = useState("") const [password, setPassword] = useState("") const [error, setError] = useState("") const [loading, setLoading] = useState(false) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError("") setLoading(true) try { const result = await signIn("credentials", { email, password, redirect: false, }) console.log("Sign in result:", { result, error: result?.error, ok: result?.ok, url: result?.url }) if (result?.error) { setError("Invalid email or password") } else if (result?.ok) { // Force a session refresh before redirect // This ensures the session cookie is properly set and read await fetch("/api/auth/session", { method: "GET" }) // Check if there's a callback URL in the query params const params = new URLSearchParams(window.location.search) const callbackUrl = params.get("callbackUrl") || "/photos" console.log("Redirecting to:", callbackUrl) // Small delay to ensure cookie is set setTimeout(() => { // Use window.location.href to force a full page reload // This ensures the session cookie is read before middleware checks authentication window.location.href = callbackUrl }, 100) } else { setError("Login failed. Please try again.") } } catch { setError("An error occurred. Please try again.") } finally { setLoading(false) } } return (

Sign in to MirrorMatch

setEmail(e.target.value)} className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 bg-white text-gray-900 rounded-t-md focus:outline-none focus:ring-purple-500 focus:border-purple-500 focus:z-10 sm:text-sm" placeholder="Email address" />
setPassword(e.target.value)} className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 bg-white text-gray-900 rounded-b-md focus:outline-none focus:ring-purple-500 focus:border-purple-500 focus:z-10 sm:text-sm" placeholder="Password" />
{error && (

{error}

)}
) }