54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { z } from "zod";
|
||
import { emailSchema, passwordSchema } from "../../../lib/validation";
|
||
|
||
/**
|
||
* Auth form schemas — validated with TanStack Form
|
||
* (`@tanstack/react-form` + `zodValidator` from `@tanstack/zod-form-adapter`).
|
||
*
|
||
* Rules mirror the server-side config in `apps/worker/src/lib/auth/auth-options.ts`
|
||
* (email/password + username plugin):
|
||
* - password minimum 8 chars (`minPasswordLength: 8`)
|
||
* - username 3–30 chars, alphanumeric + underscores (username plugin default)
|
||
*/
|
||
|
||
export const firstNameSchema = z
|
||
.string()
|
||
.trim()
|
||
.min(1, "First name is required")
|
||
.max(50, "First name is too long");
|
||
|
||
export const lastNameSchema = z
|
||
.string()
|
||
.trim()
|
||
.min(1, "Last name is required")
|
||
.max(50, "Last name is too long");
|
||
|
||
export const usernameSchema = z
|
||
.string()
|
||
.trim()
|
||
.refine((value) => value === "" || /^[a-zA-Z0-9_]{3,30}$/.test(value), {
|
||
message: "3–30 characters — letters, numbers and underscores only",
|
||
});
|
||
|
||
export const loginSchema = z.object({
|
||
email: emailSchema,
|
||
password: passwordSchema,
|
||
});
|
||
|
||
export const signupSchema = z
|
||
.object({
|
||
firstName: firstNameSchema,
|
||
lastName: lastNameSchema,
|
||
email: emailSchema,
|
||
username: usernameSchema,
|
||
password: passwordSchema,
|
||
confirmPassword: z.string(),
|
||
})
|
||
.refine((data) => data.password === data.confirmPassword, {
|
||
message: "Passwords do not match",
|
||
path: ["confirmPassword"],
|
||
});
|
||
|
||
export type SignupInput = z.infer<typeof signupSchema>;
|
||
export type LoginInput = z.infer<typeof loginSchema>;
|