Files
co-lite/solvway-admin-portal/app/reset-password/page.tsx
T

91 lines
3.3 KiB
TypeScript

"use client";
import { useState } from "react";
import { Loader2 } from "lucide-react";
import { useRouter } from "next/navigation";
import { Button } from "@/app/components/ui/button";
import { Input } from "@/app/components/ui/input";
import { Label } from "@/app/components/ui/label";
import { ErrorState } from "@/app/components/ui/states";
import { apiPost } from "@/app/lib/clientApi";
import { useAdminAuth } from "@/app/lib/AdminAuthContext";
import { useT } from "@/app/lib/i18n/MessagesProvider";
import AuthFrame from "@/app/components/app/AuthFrame";
// First sign-in: login answered resetPasswordRequired (the account is still on the temporary
// password an admin set), so the user must choose their own before reaching the portal.
export default function ResetPasswordPage() {
const router = useRouter();
const { refresh } = useAdminAuth();
const t = useT();
const [newPassword, setNewPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [error, setError] = useState<string | null>(null);
const [submitting, setSubmitting] = useState(false);
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
setError(null);
if (!newPassword || !confirmPassword) {
setError("ResetAllRequired");
return;
}
if (newPassword !== confirmPassword) {
setError("ResetMismatch");
return;
}
setSubmitting(true);
try {
await apiPost("/api/auth/first-login", { newPassword });
await refresh();
router.push("/");
} catch (err) {
setError(err instanceof Error ? err.message : "ResetFailed");
} finally {
setSubmitting(false);
}
}
return (
<AuthFrame>
<div className="space-y-2">
<h1 className="font-heading text-3xl font-semibold tracking-tight">{t("ResetTitle")}</h1>
<p className="text-muted-foreground">{t("ResetChooseIntro")}</p>
</div>
{error && <ErrorState message={t(error)} />}
<form onSubmit={handleSubmit} className="space-y-5" noValidate>
<div className="space-y-1.5">
<Label htmlFor="newPassword">{t("ResetNewPassword")}</Label>
<Input
id="newPassword"
type="password"
autoComplete="new-password"
value={newPassword}
onChange={(e) => setNewPassword(e.target.value)}
disabled={submitting}
aria-describedby="newPasswordRules"
className="h-11 bg-card"
/>
<p id="newPasswordRules" className="text-sm text-muted-foreground">{t("AdminPasswordHint")}</p>
</div>
<div className="space-y-1.5">
<Label htmlFor="confirmPassword">{t("ResetConfirmPassword")}</Label>
<Input
id="confirmPassword"
type="password"
autoComplete="new-password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
disabled={submitting}
className="h-11 bg-card"
/>
</div>
<Button type="submit" className="h-11 w-full text-base shadow-md" disabled={submitting}>
{submitting && <Loader2 aria-hidden="true" className="size-4 animate-spin" />}
{t("ResetSubmit")}
</Button>
</form>
</AuthFrame>
);
}