Skip to content
refs
History
51 lines · 2.1 KBRaw
1import { Button, Input } from "@refs/design-system";
2import { Form, Link, redirect, useNavigation } from "react-router";
3import type { Route } from "./+types/forgot-password";
4import { AuthPage } from "../components/shell";
5import { failed, formText, gate, refs } from "../lib/session";
6
7export const meta: Route.MetaFunction = () => [{ title: "Reset your password · refs" }];
8
9export function loader({ context }: Route.LoaderArgs) {
10 if (refs(context).viewer.person) throw redirect("/");
11 return null;
12}
13
14export async function action({ request, context }: Route.ActionArgs) {
15 const email = formText(await request.formData(), "email").toLowerCase();
16 const outcome = await gate(context).requestPasswordReset({ email, ip: request.headers.get("cf-connecting-ip") ?? "unknown" });
17 if (!outcome.ok) return failed(outcome.problem, { email, sent: false });
18 return { email, sent: true, error: undefined };
19}
20
21export default function ForgotPassword({ actionData }: Route.ComponentProps) {
22 const submitting = useNavigation().state === "submitting";
23 if (actionData?.sent) {
24 return (
25 <AuthPage subtitle="Check your email">
26 <p className="m-0 text-pretty">
27 If <strong>{actionData.email}</strong> has a refs account, a reset link is on its way. The link expires in 1 hour.
28 </p>
29 <Link to="/login" className="text-sm text-accent-text">
30 Back to sign in
31 </Link>
32 </AuthPage>
33 );
34 }
35 return (
36 <AuthPage subtitle="Reset your password" error={actionData?.error}>
37 <Form method="post" className="flex flex-col gap-4">
38 <Input label="Email" name="email" type="email" size="lg" defaultValue={actionData?.email} autoComplete="username" description="Enter the email of your account. A reset link goes to it." required autoFocus />
39 <Button type="submit" variant="primary" size="lg" className="w-full justify-center" loading={submitting}>
40 Send reset link
41 </Button>
42 <p className="m-0 text-center text-sm text-muted">
43 Remember it?{" "}
44 <Link to="/login" className="text-accent-text">
45 Sign in
46 </Link>
47 </p>
48 </Form>
49 </AuthPage>
50 );
51}