-
Notifications
You must be signed in to change notification settings - Fork 0
Add user creation form with validation and update README #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d1afb06
style(auth): Capitalize workspace badge label
YuDaChao efc0970
feat(user-create): add user creation form with validation flow
YuDaChao 3421d0e
feat: 修改createUser UI
YuDaChao d16543a
Update README.md
YuDaChao 196d83b
Merge pull request #5 from yudc13/feat/home
yudc13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| import { apiError, apiOk } from "@/lib/api/response"; | ||
| import { createUserRoleOptions } from "@/features/user-create/schema"; | ||
|
|
||
| const createUserPayloadSchema = z.object({ | ||
| username: z.string().trim().min(3).max(20), | ||
| email: z.string().trim().email(), | ||
| role: z.enum(createUserRoleOptions), | ||
| password: z.string().min(8), | ||
| }); | ||
|
|
||
| export async function POST(request: Request) { | ||
| let payload: unknown; | ||
|
|
||
| try { | ||
| payload = await request.json(); | ||
| } catch { | ||
| payload = {}; | ||
| } | ||
|
|
||
| const parsed = createUserPayloadSchema.safeParse(payload); | ||
| if (!parsed.success) { | ||
| return apiError("Invalid payload", "INVALID_PAYLOAD", 400); | ||
| } | ||
|
|
||
| return apiOk( | ||
| { | ||
| user: { | ||
| id: crypto.randomUUID(), | ||
| username: parsed.data.username, | ||
| email: parsed.data.email, | ||
| role: parsed.data.role, | ||
| }, | ||
| }, | ||
| 201, | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { UserCreateForm } from "@/features/user-create/components/user-create-form"; | ||
|
|
||
| export default function UserCreatePage() { | ||
| return ( | ||
| <main className="min-h-screen bg-[#000000] text-[#E8E8E8]"> | ||
| <div className="relative mx-auto w-full max-w-4xl px-6 py-12"> | ||
| <div | ||
| className="pointer-events-none absolute inset-0 opacity-20" | ||
| style={{ | ||
| backgroundImage: "radial-gradient(circle, #333333 1px, transparent 1px)", | ||
| backgroundSize: "16px 16px", | ||
| }} | ||
| aria-hidden | ||
| /> | ||
| <UserCreateForm /> | ||
| </div> | ||
| </main> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { requestJson } from "@/features/shared/api/request-json"; | ||
| import type { CreateUserPayload, CreateUserRole } from "@/features/user-create/schema"; | ||
|
|
||
| export type CreateUserResponse = { | ||
| ok: true; | ||
| user: { | ||
| id: string; | ||
| username: string; | ||
| email: string; | ||
| role: CreateUserRole; | ||
| }; | ||
| }; | ||
|
|
||
| export async function createUser(payload: CreateUserPayload) { | ||
| return requestJson<CreateUserResponse>("/api/users", { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify(payload), | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| "use client"; | ||
|
|
||
| import { useMemo, useState } from "react"; | ||
| import { useForm } from "react-hook-form"; | ||
| import { zodResolver } from "@hookform/resolvers/zod"; | ||
|
|
||
| import { Button } from "@/components/ui/button"; | ||
| import { Input } from "@/components/ui/input"; | ||
| import { createUser } from "@/features/user-create/api/create-user"; | ||
| import { | ||
| createUserFormSchema, | ||
| createUserRoleOptions, | ||
| type CreateUserFormValues, | ||
| } from "@/features/user-create/schema"; | ||
| import { createSubmitCreateUser } from "@/features/user-create/submit"; | ||
|
|
||
| const defaultValues: CreateUserFormValues = { | ||
| username: "", | ||
| email: "", | ||
| role: "", | ||
| password: "", | ||
| confirmPassword: "", | ||
| }; | ||
|
|
||
| type InlineStatus = { | ||
| tone: "neutral" | "success" | "error"; | ||
| text: string; | ||
| }; | ||
|
|
||
| export function UserCreateForm() { | ||
| const [status, setStatus] = useState<InlineStatus | null>(null); | ||
| const form = useForm<CreateUserFormValues>({ | ||
| resolver: zodResolver(createUserFormSchema), | ||
| defaultValues, | ||
| }); | ||
|
|
||
| const submitCreateUser = useMemo( | ||
| () => | ||
| createSubmitCreateUser({ | ||
| createUser, | ||
| onSuccess: () => { | ||
| setStatus({ tone: "success", text: "[SAVED]" }); | ||
| }, | ||
| onError: (message) => { | ||
| setStatus({ tone: "error", text: `[ERROR: ${message || "创建用户失败"}]` }); | ||
| }, | ||
| }), | ||
| [], | ||
| ); | ||
|
|
||
| const onSubmit = form.handleSubmit(async (values) => { | ||
| const result = await submitCreateUser(values); | ||
|
|
||
| if (!result.ok) { | ||
| if (result.reason === "submitting") { | ||
| setStatus({ tone: "neutral", text: "[LOADING...]" }); | ||
| } | ||
| if (result.reason === "validation" && result.fieldErrors) { | ||
| for (const [name, messages] of Object.entries(result.fieldErrors)) { | ||
| if (!messages?.length) continue; | ||
| form.setError(name as keyof CreateUserFormValues, { message: messages[0] }); | ||
| } | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| form.reset(defaultValues); | ||
| }); | ||
|
|
||
| return ( | ||
| <form | ||
| onSubmit={onSubmit} | ||
| className="relative grid gap-8 rounded-2xl border border-[#333333] bg-[#111111] p-6 md:p-8" | ||
| style={{ fontFamily: "var(--font-space-grotesk)" }} | ||
| > | ||
| <div className="grid gap-2"> | ||
| <p | ||
| className="text-[11px] tracking-[0.08em] text-[#999999] uppercase" | ||
| style={{ fontFamily: "var(--font-space-mono)" }} | ||
| > | ||
| SYSTEM / USER PROVISION | ||
| </p> | ||
| <h1 className="text-4xl leading-none font-medium tracking-[-0.02em] text-[#FFFFFF] md:text-5xl"> | ||
| CREATE USER | ||
| </h1> | ||
| <p className="max-w-xl text-sm text-[#999999]"> | ||
| 使用严格参数创建用户记录。输入值会在提交前执行本地校验,确认后写入 API payload。 | ||
| </p> | ||
| </div> | ||
|
|
||
| <div className="grid gap-5 md:grid-cols-2"> | ||
| <div className="grid gap-2"> | ||
| <label | ||
| htmlFor="username" | ||
| className="text-[11px] tracking-[0.08em] text-[#999999] uppercase" | ||
| style={{ fontFamily: "var(--font-space-mono)" }} | ||
| > | ||
| USERNAME | ||
| </label> | ||
| <Input | ||
| id="username" | ||
| placeholder="operator_01" | ||
| className="h-11 rounded-lg border-[#333333] bg-[#111111] px-0 text-[15px] text-[#E8E8E8] shadow-none [font-family:var(--font-space-mono)] placeholder:text-[#666666] focus-visible:border-[#E8E8E8] focus-visible:ring-0" | ||
| {...form.register("username")} | ||
| /> | ||
| {form.formState.errors.username ? ( | ||
| <p | ||
| className="text-[12px] tracking-[0.04em] text-[#D71921] uppercase" | ||
| style={{ fontFamily: "var(--font-space-mono)" }} | ||
| > | ||
| [ERROR: {form.formState.errors.username.message}] | ||
| </p> | ||
| ) : null} | ||
| </div> | ||
|
|
||
| <div className="grid gap-2"> | ||
| <label | ||
| htmlFor="email" | ||
| className="text-[11px] tracking-[0.08em] text-[#999999] uppercase" | ||
| style={{ fontFamily: "var(--font-space-mono)" }} | ||
| > | ||
| </label> | ||
| <Input | ||
| id="email" | ||
| type="email" | ||
| placeholder="user@example.com" | ||
| className="h-11 rounded-lg border-[#333333] bg-[#111111] px-0 text-[15px] text-[#E8E8E8] shadow-none [font-family:var(--font-space-mono)] placeholder:text-[#666666] focus-visible:border-[#E8E8E8] focus-visible:ring-0" | ||
| {...form.register("email")} | ||
| /> | ||
| {form.formState.errors.email ? ( | ||
| <p | ||
| className="text-[12px] tracking-[0.04em] text-[#D71921] uppercase" | ||
| style={{ fontFamily: "var(--font-space-mono)" }} | ||
| > | ||
| [ERROR: {form.formState.errors.email.message}] | ||
| </p> | ||
| ) : null} | ||
| </div> | ||
| </div> | ||
|
|
||
| <div className="grid gap-5 md:grid-cols-3"> | ||
| <div className="grid gap-2 md:col-span-1"> | ||
| <label | ||
| htmlFor="role" | ||
| className="text-[11px] tracking-[0.08em] text-[#999999] uppercase" | ||
| style={{ fontFamily: "var(--font-space-mono)" }} | ||
| > | ||
| ROLE | ||
| </label> | ||
| <select | ||
| id="role" | ||
| className="h-11 rounded-lg border border-[#333333] bg-[#111111] px-3 text-[14px] text-[#E8E8E8] outline-none transition-colors [font-family:var(--font-space-mono)] focus:border-[#E8E8E8]" | ||
| {...form.register("role")} | ||
| > | ||
| <option className="bg-[#111111]" value=""> | ||
| SELECT | ||
| </option> | ||
| {createUserRoleOptions.map((role) => ( | ||
| <option className="bg-[#111111]" key={role} value={role}> | ||
| {role.toUpperCase()} | ||
| </option> | ||
| ))} | ||
| </select> | ||
| {form.formState.errors.role ? ( | ||
| <p | ||
| className="text-[12px] tracking-[0.04em] text-[#D71921] uppercase" | ||
| style={{ fontFamily: "var(--font-space-mono)" }} | ||
| > | ||
| [ERROR: {form.formState.errors.role.message}] | ||
| </p> | ||
| ) : null} | ||
| </div> | ||
|
|
||
| <div className="grid gap-2"> | ||
| <label | ||
| htmlFor="password" | ||
| className="text-[11px] tracking-[0.08em] text-[#999999] uppercase" | ||
| style={{ fontFamily: "var(--font-space-mono)" }} | ||
| > | ||
| PASSWORD | ||
| </label> | ||
| <Input | ||
| id="password" | ||
| type="password" | ||
| placeholder="••••••••" | ||
| className="h-11 rounded-lg border-[#333333] bg-[#111111] px-0 text-[15px] text-[#E8E8E8] shadow-none [font-family:var(--font-space-mono)] placeholder:text-[#666666] focus-visible:border-[#E8E8E8] focus-visible:ring-0" | ||
| {...form.register("password")} | ||
| /> | ||
| {form.formState.errors.password ? ( | ||
| <p | ||
| className="text-[12px] tracking-[0.04em] text-[#D71921] uppercase" | ||
| style={{ fontFamily: "var(--font-space-mono)" }} | ||
| > | ||
| [ERROR: {form.formState.errors.password.message}] | ||
| </p> | ||
| ) : null} | ||
| </div> | ||
|
|
||
| <div className="grid gap-2"> | ||
| <label | ||
| htmlFor="confirmPassword" | ||
| className="text-[11px] tracking-[0.08em] text-[#999999] uppercase" | ||
| style={{ fontFamily: "var(--font-space-mono)" }} | ||
| > | ||
| CONFIRM | ||
| </label> | ||
| <Input | ||
| id="confirmPassword" | ||
| type="password" | ||
| placeholder="••••••••" | ||
| className="h-11 rounded-lg border-[#333333] bg-[#111111] px-0 text-[15px] text-[#E8E8E8] shadow-none [font-family:var(--font-space-mono)] placeholder:text-[#666666] focus-visible:border-[#E8E8E8] focus-visible:ring-0" | ||
| {...form.register("confirmPassword")} | ||
| /> | ||
| {form.formState.errors.confirmPassword ? ( | ||
| <p | ||
| className="text-[12px] tracking-[0.04em] text-[#D71921] uppercase" | ||
| style={{ fontFamily: "var(--font-space-mono)" }} | ||
| > | ||
| [ERROR: {form.formState.errors.confirmPassword.message}] | ||
| </p> | ||
| ) : null} | ||
| </div> | ||
| </div> | ||
|
|
||
| <div className="flex items-center justify-between border-t border-[#222222] pt-5"> | ||
| <p | ||
| className={`text-[11px] tracking-[0.08em] uppercase ${ | ||
| status?.tone === "error" | ||
| ? "text-[#D71921]" | ||
| : status?.tone === "success" | ||
| ? "text-[#E8E8E8]" | ||
| : "text-[#666666]" | ||
| }`} | ||
| style={{ fontFamily: "var(--font-space-mono)" }} | ||
| > | ||
| {status?.text ?? "[READY]"} | ||
| </p> | ||
|
|
||
| <Button | ||
| type="submit" | ||
| disabled={form.formState.isSubmitting} | ||
| className="h-11 rounded-full border border-[#E8E8E8] bg-[#E8E8E8] px-6 text-[12px] tracking-[0.08em] text-[#000000] uppercase transition-colors [font-family:var(--font-space-mono)] hover:bg-[#FFFFFF]" | ||
| > | ||
| {form.formState.isSubmitting ? "SUBMITTING" : "CREATE USER"} | ||
| </Button> | ||
| </div> | ||
| </form> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
POST /api/userscurrently validates input and immediately returns success withcrypto.randomUUID(), but there is no call to Clerk or any DB/repository write in this handler. In practice, the form shows a successful creation while no real account exists, so downstream flows that rely on the returneduser.idwill point to a non-existent user. This should perform an actual create operation (or surface a failure) before returningapiOk.Useful? React with 👍 / 👎.