Files
crosscode-platform/apps/accounts/frontend/src/app/reset/Reset.tsx
T

36 lines
1.7 KiB
TypeScript

"use client";
import { KeyRound } from "lucide-react";
import React, { useState } from "react";
import { Button, Card, Field, Heading, Notice } from "@crosscode/ui";
import { PublicShell } from "@/components/PublicShell";
import { useBranding } from "@/components/BrandingProvider";
import { call, selfSignIn } from "@/lib/api";
export function Reset({ token }: { token: string | null }) {
const branding = useBranding();
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
return (
<PublicShell>
<Card>
<Heading icon={<KeyRound className="h-5 w-5" />} sub="At least 8 characters.">Choose a new password</Heading>
{error && <Notice tone="error">{error}</Notice>}
<form onSubmit={async (e) => {
e.preventDefault();
setBusy(true);
const r = await call<{ uid: string | null; returnUrl: string | null }>("POST", "/password/reset", { token, password: new FormData(e.currentTarget).get("password") });
setBusy(false);
if (!r.ok) return setError(r.error.message);
// Back to the sign-in it started from (and on to that site), to the product that made the
// account (its sign-in brings them straight back here signed in), or to the account page.
window.location.assign(r.data.uid ? `/signin?uid=${encodeURIComponent(r.data.uid)}` : r.data.returnUrl ?? selfSignIn("/account"));
}}>
<Field label="New password" name="password" type="password" autoComplete="new-password" required minLength={8} autoFocus />
<Button type="submit" busy={busy} className="w-full">Save and sign in</Button>
</form>
</Card>
</PublicShell>
);
}