From 5c1a60645fecc6d3d293e868a79dd95819c4ba46 Mon Sep 17 00:00:00 2001 From: smanylov Date: Fri, 28 Nov 2025 12:38:14 +0700 Subject: [PATCH] refactor(user-register): showing validation --- src/app/actions/auth.ts | 28 +++++++++++++++++++++------- src/app/lib/definitions.ts | 33 ++++++++++++++++++++------------- src/app/ui/register-form.tsx | 25 ++++++++++++++++++++----- 3 files changed, 61 insertions(+), 25 deletions(-) diff --git a/src/app/actions/auth.ts b/src/app/actions/auth.ts index cf3bb84..c2e6d5a 100644 --- a/src/app/actions/auth.ts +++ b/src/app/actions/auth.ts @@ -23,35 +23,49 @@ export async function authorization( } export async function registration( - state: string | undefined, + state: Record | undefined, formData: FormData, ) { const validatedFields = SignupFormSchema.safeParse({ - name: formData.get('full_name'), + full_name: formData.get('full_name'), email: formData.get('email'), password: formData.get('password'), + confirm_password: formData.get('confirm_password'), }) if (!validatedFields.success) { - return 'Something went wrong from valid.'; + const errors: Record = {} + JSON.parse(validatedFields.error.message).forEach((obj: { + message: string, + path: string + }) => { + if (errors[obj.path[0]]) { + errors[obj.path[0]] = `${errors[obj.path[0]]} ${obj.message}`; + } else { + errors[obj.path[0]] = obj.message; + } + }); + + return errors; } try { - const { name, email, password } = validatedFields.data; + const { full_name, email, password } = validatedFields.data; const response = await fetch('http://localhost:8080/api/auth/register', { method: 'POST', - body: JSON.stringify({ fullName: name, email, password }), + body: JSON.stringify({ fullName: full_name, email, password }), headers: { "Content-Type": "application/json", "Accept": "application/json" } }); - let parsed = await response.json(); await createSession(parsed.token); } catch (error) { if (error) { - return 'Something went wrong. error'; + const errors: Record = {} + errors['server'] = 'Something went wrong. from server' + return errors; } throw error; } diff --git a/src/app/lib/definitions.ts b/src/app/lib/definitions.ts index f91c601..66dbf87 100644 --- a/src/app/lib/definitions.ts +++ b/src/app/lib/definitions.ts @@ -1,18 +1,25 @@ import * as z from 'zod' -export const SignupFormSchema = z.object({ - name: z - .string() - .min(2, { error: 'Name must be at least 2 characters long.' }) - .trim(), - email: z.email({ error: 'Please enter a valid email.' }).trim(), - password: z - .string() - .min(5, { error: 'Be at least 5 characters long' }) - .regex(/[a-zA-Z]/, { error: 'Contain at least one letter.' }) - .regex(/[0-9]/, { error: 'Contain at least one number.' }) - .trim(), -}) +export const SignupFormSchema = z + .object({ + full_name: z + .string() + .min(3, { error: 'Name must be at least 3 characters long.' }) + .trim(), + email: z.email({ error: 'Please enter a valid email.' }).trim(), + password: z + .string() + .min(6, { error: 'Be at least 6 characters long' }) + .regex(/[a-zA-Z]/, { error: 'Contain at least one letter.' }) + .regex(/[0-9]/, { error: 'Contain at least one number.' }) + .trim(), + confirm_password: z + .string() + }) + .refine((data) => data.password === data.confirm_password, { + message: 'passwordMismatchErrorMessage', + path: ['confirm_password'], + }); export type FormState = | { diff --git a/src/app/ui/register-form.tsx b/src/app/ui/register-form.tsx index 538859c..e34f2d6 100644 --- a/src/app/ui/register-form.tsx +++ b/src/app/ui/register-form.tsx @@ -1,7 +1,7 @@ 'use client' import styles from '@/app/styles/login.module.scss'; -import { useActionState } from 'react'; +import { useActionState, useEffect } from 'react'; import { registration } from '@/app/actions/auth'; export default function RegisterForm() { @@ -10,8 +10,14 @@ export default function RegisterForm() { undefined, ); + useEffect(() => { + console.log(errorMessage); + }, [errorMessage]) + return ( -
+ { + formAction(e); + }}>
+ {errorMessage?.email && ( +

{errorMessage!.email}

+ )}
@@ -42,15 +54,18 @@ export default function RegisterForm() {
+ {errorMessage?.password && ( +

{errorMessage!.password}

+ )}
+ {errorMessage?.confirm_password && ( +

{errorMessage!.confirm_password}

+ )}
- {errorMessage && ( -

{errorMessage}

- )}