Fix sign-up/login not always redirecting to profile
Two issues: - The login page had no error handling around the sign-in call, so any hiccup (slow connection, first page compile, etc.) failed silently: the button stayed stuck on "Logging in..." with no redirect and no error shown. - Both pages used a client-side router transition to move to /profile right after signing in, which can race with the session cookie landing in the browser. Switched to a full page navigation instead, which guarantees the cookie is present before /profile checks it. Verified with a 10-run automated stress test (5 signups + 5 logins, back to back with no delay) against a production build - all 10 now land on /profile reliably. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VL5YUEf2B9LG4xdVUQwcSg
This commit is contained in:
2 files changed
+14
-9
No files matched your search
@@ -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<string | null>(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") ?? "");
|
||||
|
||||
try {
|
||||
const result = await signIn("credentials", {
|
||||
redirect: false,
|
||||
email,
|
||||
password,
|
||||
});
|
||||
|
||||
if (result?.error) {
|
||||
if (!result || result.error) {
|
||||
setError("Incorrect email or password.");
|
||||
setLoading(false);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -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<string | null>(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);
|
||||
|
||||
Reference in new issue
Block a user