From d957a8d03f61ae836ffbbea1b74bb739b16ba080 Mon Sep 17 00:00:00 2001 From: B SIMAK AHMED <1NZ24CS030@newhorizonindia.edu> Date: Tue, 22 Sep 2026 22:08:10 +0530 Subject: [PATCH 1/6] Add forgot password link to admin login --- src/routes/auth.tsx | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/routes/auth.tsx b/src/routes/auth.tsx index 8916b8d..8dd78d1 100644 --- a/src/routes/auth.tsx +++ b/src/routes/auth.tsx @@ -7,9 +7,9 @@ * - If the admin has a verified TOTP factor → /mfa-verify * - Otherwise → /mfa-setup */ -import { createFileRoute, useNavigate } from "@tanstack/react-router"; +import { createFileRoute, Link, useNavigate } from "@tanstack/react-router"; import { useEffect, useState } from "react"; -import { Code2, Mail, Lock, Eye, EyeOff, Info } from "lucide-react"; +import { Code2, Mail, Lock, Eye, EyeOff, Info, KeyRound } from "lucide-react"; import { SiteLayout } from "@/components/SiteLayout"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; @@ -215,6 +215,15 @@ function AuthPage() { > {busy ? "Signing in…" : isLocked ? "Locked" : "Login"} +
+ + + Forgot Password? + +
From 3a5467d05d9b63f67dd534f364b92c3ad7de22d0 Mon Sep 17 00:00:00 2001 From: B SIMAK AHMED <1NZ24CS030@newhorizonindia.edu> Date: Tue, 22 Sep 2026 22:08:27 +0530 Subject: [PATCH 2/6] Add secure admin password recovery request --- src/routes/forgot-password.tsx | 141 +++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 src/routes/forgot-password.tsx diff --git a/src/routes/forgot-password.tsx b/src/routes/forgot-password.tsx new file mode 100644 index 0000000..bc4b6e7 --- /dev/null +++ b/src/routes/forgot-password.tsx @@ -0,0 +1,141 @@ +/** + * /forgot-password — Admin password recovery request. + * + * Supabase sends the recovery email. The page deliberately returns the same + * success message regardless of whether the address exists to avoid account + * enumeration. + */ +import { createFileRoute, Link, useNavigate } from "@tanstack/react-router"; +import { useState } from "react"; +import { ArrowLeft, CheckCircle2, Code2, Info, KeyRound, Mail } from "lucide-react"; +import { SiteLayout } from "@/components/SiteLayout"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { TurnstileWidget } from "@/components/TurnstileWidget"; +import { supabase } from "@/integrations/supabase/client"; +import { toast } from "sonner"; + +export const Route = createFileRoute("/forgot-password")({ + head: () => ({ + meta: [ + { title: "Forgot Password — CodeForge" }, + { name: "description", content: "Request a secure password reset link for CodeForge administrator access." }, + { name: "robots", content: "noindex, nofollow" }, + ], + links: [{ rel: "canonical", href: "https://codeforgedev.vercel.app/forgot-password" }], + }), + component: ForgotPasswordPage, +}); + +function ForgotPasswordPage() { + const navigate = useNavigate(); + const [email, setEmail] = useState(""); + const [turnstileToken, setTurnstileToken] = useState(""); + const [resetKey, setResetKey] = useState(0); + const [busy, setBusy] = useState(false); + const [sent, setSent] = useState(false); + + const submit = async (event: React.FormEvent) => { + event.preventDefault(); + const normalizedEmail = email.trim().toLowerCase(); + if (!normalizedEmail || !turnstileToken) return; + + setBusy(true); + try { + const redirectTo = `${window.location.origin}/reset-password`; + const { error } = await supabase.auth.resetPasswordForEmail(normalizedEmail, { redirectTo }); + + if (error) { + throw error; + } + + setSent(true); + setTurnstileToken(""); + setResetKey((key) => key + 1); + } catch { + toast.error("Unable to process the request. Please try again.", { + icon: , + }); + } finally { + setBusy(false); + } + }; + + return ( + +
+
+
+
+ +
+

Forgot Password?

+

+ Request a secure password reset link for your CodeForge administrator account. +

+
+ + {sent ? ( +
+
+ +

Check your email

+

+ If an account uses that email address, a password reset link has been sent. + The link is time-limited and can only be used through CodeForge. +

+
+ + + Back to Admin Login + +
+ ) : ( +
+
+ +
+ + setEmail(event.target.value)} + placeholder="admin@domain.com" + autoComplete="email" + required + className="h-13 rounded-xl border-border/80 bg-background/60 pl-11" + /> +
+
+ + + + + +
+ + + Back to Admin Login + +
+ + )} +
+
+
+ ); +} From 981065bed5f812a13908eb5de4ac2d9b7aeab69b Mon Sep 17 00:00:00 2001 From: B SIMAK AHMED <1NZ24CS030@newhorizonindia.edu> Date: Tue, 22 Sep 2026 22:08:48 +0530 Subject: [PATCH 3/6] Add secure password reset completion flow --- src/routes/reset-password.tsx | 220 ++++++++++++++++++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 src/routes/reset-password.tsx diff --git a/src/routes/reset-password.tsx b/src/routes/reset-password.tsx new file mode 100644 index 0000000..16ad0d5 --- /dev/null +++ b/src/routes/reset-password.tsx @@ -0,0 +1,220 @@ +/** + * /reset-password — Recovery-session password update. + * + * Supabase establishes a recovery session from the email link. We only allow + * the password update when a valid authenticated recovery session exists, then + * sign out and send the administrator through the normal MFA login flow. + */ +import { createFileRoute, Link, useNavigate } from "@tanstack/react-router"; +import { useEffect, useState } from "react"; +import { ArrowLeft, CheckCircle2, Code2, Eye, EyeOff, Info, Lock } from "lucide-react"; +import { SiteLayout } from "@/components/SiteLayout"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { supabase } from "@/integrations/supabase/client"; +import { toast } from "sonner"; + +export const Route = createFileRoute("/reset-password")({ + head: () => ({ + meta: [ + { title: "Reset Password — CodeForge" }, + { name: "description", content: "Set a new password for CodeForge administrator access." }, + { name: "robots", content: "noindex, nofollow" }, + ], + links: [{ rel: "canonical", href: "https://codeforgedev.vercel.app/reset-password" }], + }), + component: ResetPasswordPage, +}); + +function ResetPasswordPage() { + const navigate = useNavigate(); + const [ready, setReady] = useState(false); + const [valid, setValid] = useState(false); + const [password, setPassword] = useState(""); + const [confirmation, setConfirmation] = useState(""); + const [showPassword, setShowPassword] = useState(false); + const [busy, setBusy] = useState(false); + const [complete, setComplete] = useState(false); + + useEffect(() => { + let active = true; + + const checkSession = async () => { + const { data, error } = await supabase.auth.getSession(); + if (!active) return; + setValid(!error && !!data.session); + setReady(true); + }; + + void checkSession(); + + const { data: listener } = supabase.auth.onAuthStateChange((event, session) => { + if (!active) return; + if (event === "PASSWORD_RECOVERY" || session) { + setValid(true); + setReady(true); + } + }); + + return () => { + active = false; + listener.subscription.unsubscribe(); + }; + }, []); + + const submit = async (event: React.FormEvent) => { + event.preventDefault(); + + if (password.length < 12) { + toast.error("Use a password with at least 12 characters.", { icon: }); + return; + } + + if (password !== confirmation) { + toast.error("Passwords do not match.", { icon: }); + return; + } + + setBusy(true); + const { error } = await supabase.auth.updateUser({ password }); + setBusy(false); + + if (error) { + toast.error("Unable to reset the password. The recovery link may have expired.", { + icon: , + }); + return; + } + + await supabase.auth.signOut(); + setPassword(""); + setConfirmation(""); + setComplete(true); + }; + + return ( + +
+
+
+
+ +
+

Reset Password

+

+ Set a new password for your CodeForge administrator account. +

+
+ + {!ready ? ( +
Verifying recovery link…
+ ) : complete ? ( +
+
+ +

Password updated

+

+ Your password has been changed. Sign in again and complete two-step verification to access the administrator dashboard. +

+
+ +
+ ) : !valid ? ( +
+
+

Invalid or expired link

+

+ Request a new password reset link and try again. +

+
+ + + Request another reset link + +
+ ) : ( +
+ setShowPassword((value) => !value)} + /> + setShowPassword((value) => !value)} + /> +

+ Minimum 12 characters. After resetting, administrator access still requires TOTP verification. +

+ + + )} +
+
+
+ ); +} + +function PasswordField({ + id, + label, + value, + onChange, + show, + onToggle, +}: { + id: string; + label: string; + value: string; + onChange: (value: string) => void; + show: boolean; + onToggle: () => void; +}) { + return ( +
+ +
+ + onChange(event.target.value)} + className="h-13 rounded-xl border-border/80 bg-background/60 pl-11 pr-11" + autoComplete={id === "new-password" ? "new-password" : "new-password"} + minLength={12} + required + /> + +
+
+ ); +} From 8254d0a1f477e3d5e4ff6f5a064f79711d7ce778 Mon Sep 17 00:00:00 2001 From: B SIMAK AHMED <1NZ24CS030@newhorizonindia.edu> Date: Tue, 22 Sep 2026 22:09:17 +0530 Subject: [PATCH 4/6] Add protected password reset request function --- src/lib/password-reset.functions.ts | 49 +++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/lib/password-reset.functions.ts diff --git a/src/lib/password-reset.functions.ts b/src/lib/password-reset.functions.ts new file mode 100644 index 0000000..0f95f06 --- /dev/null +++ b/src/lib/password-reset.functions.ts @@ -0,0 +1,49 @@ +import { createServerFn } from "@tanstack/react-start"; +import { verifyTurnstile, getClientIp } from "@/lib/turnstile.server"; + +export const requestPasswordReset = createServerFn({ method: "POST" }) + .inputValidator((data: unknown) => { + if (!data || typeof data !== "object") throw new Error("Invalid password reset request."); + const value = data as Record; + if (typeof value.email !== "string" || typeof value.turnstileToken !== "string") { + throw new Error("Invalid password reset request."); + } + return { + email: value.email.trim().toLowerCase(), + turnstileToken: value.turnstileToken, + }; + }) + .handler(async ({ data }) => { + const ip = getClientIp(); + const verified = await verifyTurnstile(data.turnstileToken, ip, "password_reset"); + if (!verified) throw new Error("Human verification failed. Please try again."); + + const { supabaseAdmin } = await import("@/integrations/supabase/client.server"); + const { data: adminRole, error: roleError } = await supabaseAdmin + .from("user_roles") + .select("user_id") + .eq("role", "admin") + .limit(1); + + if (roleError) throw new Error("Unable to process the request."); + + const { createClient } = await import("@supabase/supabase-js"); + const supabaseUrl = process.env.VITE_SUPABASE_URL || process.env.SUPABASE_URL; + const publishableKey = + process.env.VITE_SUPABASE_PUBLISHABLE_KEY || process.env.SUPABASE_PUBLISHABLE_KEY; + if (!supabaseUrl || !publishableKey) throw new Error("Supabase server configuration is incomplete."); + + const userClient = createClient(supabaseUrl, publishableKey, { + auth: { autoRefreshToken: false, persistSession: false, detectSessionInUrl: false }, + }); + + const { error } = await userClient.auth.resetPasswordForEmail(data.email, { + redirectTo: "https://codeforgedev.vercel.app/reset-password", + }); + + if (error) throw new Error("Unable to process the request."); + + // Deliberately do not reveal whether the submitted address belongs to the admin. + void adminRole; + return { success: true }; + }); From 0cb68484a8d193eb1ae94186872a4f67dc61f7c1 Mon Sep 17 00:00:00 2001 From: B SIMAK AHMED <1NZ24CS030@newhorizonindia.edu> Date: Tue, 22 Sep 2026 22:09:31 +0530 Subject: [PATCH 5/6] Use protected server-side password reset request --- src/routes/forgot-password.tsx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/routes/forgot-password.tsx b/src/routes/forgot-password.tsx index bc4b6e7..1ee234b 100644 --- a/src/routes/forgot-password.tsx +++ b/src/routes/forgot-password.tsx @@ -13,7 +13,7 @@ import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { TurnstileWidget } from "@/components/TurnstileWidget"; -import { supabase } from "@/integrations/supabase/client"; +import { requestPasswordReset } from "@/lib/password-reset.functions"; import { toast } from "sonner"; export const Route = createFileRoute("/forgot-password")({ @@ -43,12 +43,12 @@ function ForgotPasswordPage() { setBusy(true); try { - const redirectTo = `${window.location.origin}/reset-password`; - const { error } = await supabase.auth.resetPasswordForEmail(normalizedEmail, { redirectTo }); - - if (error) { - throw error; - } + await requestPasswordReset({ + data: { + email: normalizedEmail, + turnstileToken, + }, + }); setSent(true); setTurnstileToken(""); From 62cca19adebd6977f9f0754fb46a8b760d507e58 Mon Sep 17 00:00:00 2001 From: B SIMAK AHMED <1NZ24CS030@newhorizonindia.edu> Date: Tue, 22 Sep 2026 22:09:43 +0530 Subject: [PATCH 6/6] Simplify password reset request flow --- src/lib/password-reset.functions.ts | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/lib/password-reset.functions.ts b/src/lib/password-reset.functions.ts index 0f95f06..7eb4e05 100644 --- a/src/lib/password-reset.functions.ts +++ b/src/lib/password-reset.functions.ts @@ -18,15 +18,6 @@ export const requestPasswordReset = createServerFn({ method: "POST" }) const verified = await verifyTurnstile(data.turnstileToken, ip, "password_reset"); if (!verified) throw new Error("Human verification failed. Please try again."); - const { supabaseAdmin } = await import("@/integrations/supabase/client.server"); - const { data: adminRole, error: roleError } = await supabaseAdmin - .from("user_roles") - .select("user_id") - .eq("role", "admin") - .limit(1); - - if (roleError) throw new Error("Unable to process the request."); - const { createClient } = await import("@supabase/supabase-js"); const supabaseUrl = process.env.VITE_SUPABASE_URL || process.env.SUPABASE_URL; const publishableKey = @@ -43,7 +34,6 @@ export const requestPasswordReset = createServerFn({ method: "POST" }) if (error) throw new Error("Unable to process the request."); - // Deliberately do not reveal whether the submitted address belongs to the admin. - void adminRole; + // Deliberately do not reveal whether the submitted address exists. return { success: true }; });