diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index 7d269fa..32b4eb7 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -1,12 +1,10 @@ "use client"; import { useState, FormEvent } from "react"; -import { useRouter } from "next/navigation"; import { signIn } from "next-auth/react"; import Link from "next/link"; export default function LoginPage() { - const router = useRouter(); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); @@ -19,19 +17,26 @@ export default function LoginPage() { const email = String(form.get("email") ?? ""); const password = String(form.get("password") ?? ""); - const result = await signIn("credentials", { - redirect: false, - email, - password, - }); + try { + const result = await signIn("credentials", { + redirect: false, + email, + password, + }); - if (result?.error) { - setError("Incorrect email or password."); + if (!result || result.error) { + setError("Incorrect email or password."); + setLoading(false); + return; + } + + // A full page load (not a client-side transition) guarantees the + // browser has the fresh session cookie before /profile checks it. + window.location.href = "/profile"; + } catch { + setError("Something went wrong. Please try again."); setLoading(false); - return; } - - router.push("/profile"); } return ( diff --git a/src/app/signup/page.tsx b/src/app/signup/page.tsx index f95d682..74e91fe 100644 --- a/src/app/signup/page.tsx +++ b/src/app/signup/page.tsx @@ -1,12 +1,10 @@ "use client"; import { useState, FormEvent } from "react"; -import { useRouter } from "next/navigation"; import { signIn } from "next-auth/react"; import Link from "next/link"; export default function SignupPage() { - const router = useRouter(); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); @@ -43,12 +41,14 @@ export default function SignupPage() { password: payload.password, }); - if (signInResult?.error) { - router.push("/login"); + if (!signInResult || signInResult.error) { + window.location.href = "/login"; return; } - router.push("/profile"); + // A full page load (not a client-side transition) guarantees the + // browser has the fresh session cookie before /profile checks it. + window.location.href = "/profile"; } catch { setError("Something went wrong. Please try again."); setLoading(false);