139 lines
4.7 KiB
TypeScript
139 lines
4.7 KiB
TypeScript
import { useState } from "react";
|
||
import { Link } from "wouter";
|
||
import { z } from "zod";
|
||
import Container from "@mui/material/Container";
|
||
import Alert from "@mui/material/Alert";
|
||
import { apiRequest, parse, organizationSchema } from "@/lib/api";
|
||
import { clearRecoveryToken, getRecoveryToken } from "@/lib/recovery-token";
|
||
import { usePlatform } from "@/lib/platform";
|
||
import {
|
||
ActionForm,
|
||
Field,
|
||
Panel,
|
||
readText,
|
||
} from "@/components/WorkspaceTools";
|
||
import "./workspace.css";
|
||
export default function RecoveryPage({ reset = false }: { reset?: boolean }) {
|
||
const { organization, clearSession } = usePlatform();
|
||
const [complete, setComplete] = useState(false);
|
||
const token = getRecoveryToken();
|
||
return (
|
||
<section className="workspace-page">
|
||
<Container maxWidth="sm">
|
||
<h1>{reset ? "Reset Password" : "Account Recovery"}</h1>
|
||
<Panel
|
||
title={
|
||
reset ? "Choose a new password" : "Request recovery instructions"
|
||
}
|
||
>
|
||
{complete ? (
|
||
<Alert severity="success">
|
||
{reset
|
||
? "Your password was reset and previous sessions were invalidated."
|
||
: "If the account is eligible, recovery instructions will be sent."}{" "}
|
||
<Link href="/workspace">
|
||
<u>Return to sign-in</u>
|
||
</Link>
|
||
</Alert>
|
||
) : reset ? (
|
||
!token ? (
|
||
<Alert severity="warning">
|
||
This reset link is missing or invalid. Request a new recovery
|
||
link.
|
||
</Alert>
|
||
) : (
|
||
<ActionForm
|
||
label="Reset password"
|
||
read={(data) => {
|
||
const password = readText(data, "password");
|
||
if (password !== readText(data, "confirm"))
|
||
throw new Error("Passwords do not match.");
|
||
return { token, password };
|
||
}}
|
||
submit={async (value) => {
|
||
const body = z
|
||
.object({
|
||
token: z.string().regex(/^[A-Za-z0-9_-]{43}$/),
|
||
password: z.string().min(15).max(128),
|
||
})
|
||
.strict()
|
||
.parse(value);
|
||
parse(
|
||
z.null(),
|
||
await apiRequest("/auth/recovery/reset", {
|
||
method: "POST",
|
||
body,
|
||
}),
|
||
);
|
||
clearRecoveryToken();
|
||
clearSession(
|
||
"Password reset. Sign in with your new password.",
|
||
);
|
||
setComplete(true);
|
||
}}
|
||
>
|
||
<Field
|
||
name="password"
|
||
label="New password"
|
||
type="password"
|
||
maxLength={128}
|
||
helper="15–128 characters."
|
||
/>
|
||
<Field
|
||
name="confirm"
|
||
label="Confirm new password"
|
||
type="password"
|
||
maxLength={128}
|
||
/>
|
||
</ActionForm>
|
||
)
|
||
) : (
|
||
<>
|
||
<Alert severity="info">
|
||
Recovery delivery requires the backend SMTP configuration.
|
||
Acceptance does not confirm account existence or email delivery.
|
||
</Alert>
|
||
<ActionForm
|
||
label="Request recovery"
|
||
read={(data) => ({
|
||
organizationId: readText(data, "organizationId"),
|
||
email: readText(data, "email"),
|
||
})}
|
||
submit={async (value) => {
|
||
const body = z
|
||
.object({
|
||
organizationId: organizationSchema,
|
||
email: z.string().email().max(254).toLowerCase(),
|
||
})
|
||
.strict()
|
||
.parse(value);
|
||
parse(
|
||
z.object({ message: z.string() }),
|
||
await apiRequest("/auth/recovery/request", {
|
||
method: "POST",
|
||
body,
|
||
}),
|
||
);
|
||
setComplete(true);
|
||
}}
|
||
>
|
||
<Field
|
||
name="organizationId"
|
||
label="Organization UUID"
|
||
value={organization}
|
||
/>
|
||
<Field
|
||
name="email"
|
||
label="Account email"
|
||
type="email"
|
||
maxLength={254}
|
||
/>
|
||
</ActionForm>
|
||
</>
|
||
)}
|
||
</Panel>
|
||
</Container>
|
||
</section>
|
||
);
|
||
}
|