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";
|
"use client";
|
||||||
|
|
||||||
import { useState, FormEvent } from "react";
|
import { useState, FormEvent } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
|
||||||
import { signIn } from "next-auth/react";
|
import { signIn } from "next-auth/react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
|
|
||||||
export default function LoginPage() {
|
export default function LoginPage() {
|
||||||
const router = useRouter();
|
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
@@ -19,19 +17,26 @@ export default function LoginPage() {
|
|||||||
const email = String(form.get("email") ?? "");
|
const email = String(form.get("email") ?? "");
|
||||||
const password = String(form.get("password") ?? "");
|
const password = String(form.get("password") ?? "");
|
||||||
|
|
||||||
|
try {
|
||||||
const result = await signIn("credentials", {
|
const result = await signIn("credentials", {
|
||||||
redirect: false,
|
redirect: false,
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (result?.error) {
|
if (!result || result.error) {
|
||||||
setError("Incorrect email or password.");
|
setError("Incorrect email or password.");
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
return;
|
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 (
|
return (
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useState, FormEvent } from "react";
|
import { useState, FormEvent } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
|
||||||
import { signIn } from "next-auth/react";
|
import { signIn } from "next-auth/react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
|
|
||||||
export default function SignupPage() {
|
export default function SignupPage() {
|
||||||
const router = useRouter();
|
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
@@ -43,12 +41,14 @@ export default function SignupPage() {
|
|||||||
password: payload.password,
|
password: payload.password,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (signInResult?.error) {
|
if (!signInResult || signInResult.error) {
|
||||||
router.push("/login");
|
window.location.href = "/login";
|
||||||
return;
|
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 {
|
} catch {
|
||||||
setError("Something went wrong. Please try again.");
|
setError("Something went wrong. Please try again.");
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|||||||
Reference in new issue
Block a user