From b6af54119dce4f706c4555ac6f6f0494997ca85d Mon Sep 17 00:00:00 2001 From: presidoclintonbased-alt <309302277+presidoclintonbased-alt@users.noreply.github.com> Date: Sat, 26 Sep 2026 18:24:28 +0100 Subject: [PATCH 1/2] fix(auth): redirect signed-in users away from /login (#50) Add useRedirectIfAuthenticated: once auth has finished loading, a signed-in user is sent to /dashboard via router.replace (so the form isn't left in history), and the page renders nothing meanwhile so the form never flashes. Use it on the login page. --- src/app/login/page.tsx | 5 +++++ src/lib/use-redirect-if-authenticated.ts | 27 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/lib/use-redirect-if-authenticated.ts diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index 4c347e1..572ff92 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -5,6 +5,7 @@ import { useRouter } from 'next/navigation'; import Link from 'next/link'; import { Mail, Lock } from 'lucide-react'; import { useAuth } from '@/lib/auth-context'; +import { useRedirectIfAuthenticated } from '@/lib/use-redirect-if-authenticated'; import { ApiError } from '@/lib/api'; import { FormError } from '@/components/form-error'; import { AuthLayout } from '@/components/auth-layout'; @@ -18,6 +19,7 @@ export default function LoginPage() { const [password, setPassword] = useState(''); const [error, setError] = useState(null); const [submitting, setSubmitting] = useState(false); + const redirecting = useRedirectIfAuthenticated(); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); @@ -37,6 +39,9 @@ export default function LoginPage() { } } + // Signed-in users are sent to their dashboard instead of seeing the form (#50). + if (redirecting) return null; + return (
diff --git a/src/lib/use-redirect-if-authenticated.ts b/src/lib/use-redirect-if-authenticated.ts new file mode 100644 index 0000000..eb2bec7 --- /dev/null +++ b/src/lib/use-redirect-if-authenticated.ts @@ -0,0 +1,27 @@ +'use client'; + +import { useEffect } from 'react'; +import { useRouter } from 'next/navigation'; +import { useAuth } from './auth-context'; + +/** Where a signed-in user lands by default (matches the post-login redirect). */ +export const DEFAULT_AUTHENTICATED_PATH = '/dashboard'; + +/** + * Guest-only pages (/login, /register): once auth has finished loading, send + * a signed-in user to their default page with `router.replace`, so the auth + * form isn't left in history (#50). + * + * Returns true while the page should render nothing — auth still loading or a + * redirect in flight — so the form never flashes for a signed-in user. + */ +export function useRedirectIfAuthenticated(to: string = DEFAULT_AUTHENTICATED_PATH): boolean { + const { user, loading } = useAuth(); + const router = useRouter(); + + useEffect(() => { + if (!loading && user) router.replace(to); + }, [loading, user, router, to]); + + return loading || user !== null; +} From fa3ee3418671757613d7d2cf098152dec47a4597 Mon Sep 17 00:00:00 2001 From: presidoclintonbased-alt <309302277+presidoclintonbased-alt@users.noreply.github.com> Date: Sat, 26 Sep 2026 18:24:37 +0100 Subject: [PATCH 2/2] fix(auth): redirect signed-in users away from /register (#50) Reuse useRedirectIfAuthenticated on the register page so an authenticated user visiting /register is replaced to /dashboard instead of seeing the sign-up form. --- src/app/register/page.tsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/app/register/page.tsx b/src/app/register/page.tsx index e40b6a6..cf00d08 100644 --- a/src/app/register/page.tsx +++ b/src/app/register/page.tsx @@ -5,6 +5,7 @@ import { useRouter } from 'next/navigation'; import Link from 'next/link'; import { User, Mail, Lock } from 'lucide-react'; import { useAuth } from '@/lib/auth-context'; +import { useRedirectIfAuthenticated } from '@/lib/use-redirect-if-authenticated'; import { ApiError } from '@/lib/api'; import { FormError } from '@/components/form-error'; import { AuthLayout } from '@/components/auth-layout'; @@ -19,6 +20,7 @@ export default function RegisterPage() { const [password, setPassword] = useState(''); const [error, setError] = useState(null); const [submitting, setSubmitting] = useState(false); + const redirecting = useRedirectIfAuthenticated(); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); @@ -38,6 +40,9 @@ export default function RegisterPage() { } } + // Signed-in users are sent to their dashboard instead of seeing the form (#50). + if (redirecting) return null; + return (