feat(auth): add sign-in/sign-up/sign-out controllers with Zod validation
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { InputParseError } from "../../entities/errors";
|
||||
import { signUpUseCase } from "../../application/use-cases/sign-up.use-case";
|
||||
|
||||
const inputSchema = z
|
||||
.object({
|
||||
username: z.string().min(3).max(31),
|
||||
password: z.string().min(6).max(255),
|
||||
confirmPassword: z.string().min(6).max(255),
|
||||
})
|
||||
.superRefine(({ password, confirmPassword }, ctx) => {
|
||||
if (confirmPassword !== password) {
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
message: "The passwords did not match",
|
||||
path: ["password"],
|
||||
});
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
message: "The passwords did not match",
|
||||
path: ["confirmPassword"],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export async function signUpController(
|
||||
input: Partial<z.infer<typeof inputSchema>>,
|
||||
): Promise<ReturnType<typeof signUpUseCase>> {
|
||||
const parsed = inputSchema.safeParse(input);
|
||||
if (!parsed.success) {
|
||||
throw new InputParseError("Invalid sign-up input", { cause: parsed.error });
|
||||
}
|
||||
return await signUpUseCase(parsed.data);
|
||||
}
|
||||
Reference in New Issue
Block a user