import { useState } from "react"; import { Link, redirect, useSearchParams } from "react-router"; import { auth } from "~/lib/auth.server"; import { authClient } from "~/lib/auth-client"; import type { Route } from "./+types/reset-password"; import { Button } from "~/components/ui/button"; import { Input } from "~/components/ui/input"; import { Label } from "~/components/ui/label"; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "~/components/ui/card"; export function meta(): Route.MetaDescriptors { return [{ title: "Reset Password - Brackt" }]; } export async function loader(args: Route.LoaderArgs) { const session = await auth.api.getSession({ headers: args.request.headers }); if (session) return redirect("/"); const token = new URL(args.request.url).searchParams.get("token"); if (!token) return redirect("/forgot-password"); return {}; } export default function ResetPasswordPage() { const [searchParams] = useSearchParams(); const token = searchParams.get("token") ?? ""; // loader redirects to /forgot-password when absent const [error, setError] = useState(null); const [loading, setLoading] = useState(false); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(null); const form = e.currentTarget; const newPassword = (form.elements.namedItem("password") as HTMLInputElement).value; const confirm = (form.elements.namedItem("confirm") as HTMLInputElement).value; if (newPassword !== confirm) { setError("Passwords do not match."); return; } setLoading(true); const { error: resetError } = await authClient.resetPassword({ newPassword, token }); if (resetError) { setError(resetError.message ?? "Reset failed. The link may have expired."); setLoading(false); } else { window.location.href = "/login"; } } return (
Choose a new password Enter a new password for your account
{error &&

{error}

}

Back to sign in

); }