refactor(user-register): showing validation

This commit is contained in:
smanylov
2025-11-28 12:38:14 +07:00
parent 39dee22b89
commit 5c1a60645f
3 changed files with 61 additions and 25 deletions
+21 -7
View File
@@ -23,35 +23,49 @@ export async function authorization(
}
export async function registration(
state: string | undefined,
state: Record<string, string> | 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<string, string> = {}
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<string, string> = {}
errors['server'] = 'Something went wrong. from server'
return errors;
}
throw error;
}