refactor(user-register): showing validation
This commit is contained in:
+21
-7
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user