Files
co-lite/solvway-admin-portal/app/users/new/page.tsx
T
ngqileandClaude Opus 5.5 d2560c6b8a fix: address the admin-password branch review
Create shows a link back to Users when the user was saved but the password
was not stored. The spec's secure-generator rule now names admin temporary
passwords only, and the reset requirement no longer implies the emailed reset
refuses the caller's own account. The design and handover note that mobile-api
must stay the only gateway client allowed to reach /user/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-30 11:27:40 +08:00

67 lines
2.2 KiB
TypeScript

"use client";
import { useState } from "react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { ApiError } from "@/app/lib/clientApi";
import { ErrorState } from "@/app/components/ui/states";
import { RequireModule } from "@/app/lib/RequireModule";
import { PageHeader } from "@/app/components/ui/page-header";
import { UserForm, type UserFormPayload } from "@/app/components/users/UserForm";
import { saveAdminUser } from "@/app/lib/services/adminUsers";
import { useT } from "@/app/lib/i18n/MessagesProvider";
// The admin types the first password (legacy backoffice flow); the user is saved with a pending
// password change and must choose their own at first sign-in. No email is sent. Once the
// mobile-api returns the created user's id, we redirect to its edit page with a `created=1` banner.
// SAVE_FAILED after the save means the user exists without a password; the error carries no id, so link
// back to the list, where the admin opens the user and sets a temporary password.
function NewUserPageContent() {
const t = useT();
const router = useRouter();
const [passwordNotStored, setPasswordNotStored] = useState(false);
async function handleSubmit(payload: UserFormPayload) {
try {
const created = await saveAdminUser(payload);
if (created.id != null) {
router.push(`/users/${created.id}?created=1`);
}
} catch (err) {
if (err instanceof ApiError && (err.data as { code?: string } | undefined)?.code === "SAVE_FAILED") {
setPasswordNotStored(true);
return;
}
throw err;
}
}
if (passwordNotStored) {
return (
<>
<PageHeader title={t("UsrNewUser")} />
<ErrorState message={t("UsrCreatedNoPassword")} className="mb-4" />
<Link href="/users" className="text-primary underline-offset-4 hover:underline">
{t("UsrBackToUsers")}
</Link>
</>
);
}
return (
<>
<PageHeader title={t("UsrNewUser")} />
<UserForm onSubmit={handleSubmit} submitLabel={t("UsrCreateUser")} withPassword />
</>
);
}
export default function NewUserPage() {
return (
<RequireModule pathname="/users">
<NewUserPageContent />
</RequireModule>
);
}