edit validation messages, saving formData

This commit is contained in:
smanylov
2025-12-07 12:53:56 +07:00
parent e469bbe031
commit 29c139001d
4 changed files with 180 additions and 50 deletions
+91 -17
View File
@@ -1,6 +1,6 @@
'use server'
import {SignupFormSchema, loginFormSchema, localDevelopmentUrl} from '@/app/lib/definitions';
import { SignupFormSchema, loginFormSchema, localDevelopmentUrl } from '@/app/lib/definitions';
import { createSession, deleteSession, getSessionData } from '@/app/lib/session';
import { redirect } from 'next/navigation';
@@ -29,16 +29,41 @@ export async function logout() {
}
export async function authorization(
state: string | undefined,
state: {
previousState: {
email: string;
}
error: Record<string, string>
} | undefined,
formData: FormData,
) {
const email = formData.get('email') as string || '';
const password = formData.get('password');
const validatedFields = loginFormSchema.safeParse({
email: formData.get('email'),
password: formData.get('password'),
email,
password
})
if (!validatedFields.success) {
return 'error';
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 {
previousState: {
email
},
error: errors
};
}
try {
@@ -58,12 +83,21 @@ export async function authorization(
let parsed = await response.json();
await createSession(parsed.token, parsed.email);
} else {
throw new Error('Something went wrong. from server');
throw new Error(`Something went wrong. Status from server ${response.status}`);
}
} catch (error) {
const errors: Record<string, string> = {}
errors['server'] = `${error}`;
if (error) {
return 'Something went wrong. from server';
return {
previousState: {
email,
password
},
error: errors
};
}
throw error;
}
@@ -71,16 +105,35 @@ export async function authorization(
redirect('/pages/dashboard');
}
type FormState = {
previousState: {
full_name: string;
email: string;
password: string;
confirm_password: string;
phone: string;
company: string;
}
error: Record<string, string>
};
export async function registration(
state: Record<string, string> | undefined,
state: FormState | undefined,
formData: FormData,
) {
): Promise<FormState> {
const full_name = formData.get('full_name') as string || '';
const email = formData.get('email') as string || '';
const password = formData.get('password') as string || '';
const confirm_password = formData.get('confirm_password') as string || '';
const phone = formData.get('phone') as string || '';
const company = formData.get('company') as string || '';
const validatedFields = SignupFormSchema.safeParse({
full_name: formData.get('full_name'),
email: formData.get('email'),
password: formData.get('password'),
confirm_password: formData.get('confirm_password'),
})
full_name,
email,
password,
confirm_password,
});
if (!validatedFields.success) {
const errors: Record<string, string> = {}
@@ -95,14 +148,24 @@ export async function registration(
}
});
return errors;
return {
previousState: {
full_name,
email,
password,
confirm_password,
phone,
company
},
error: errors
};
}
try {
const { full_name, email, password } = validatedFields.data;
const response = await fetch(`${API_BASE_URL}/v1/api/auth/register`, {
method: 'POST',
body: JSON.stringify({ fullName: full_name, email, password }),
body: JSON.stringify({ fullName: full_name, email, password, phone, company }),
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
@@ -121,7 +184,18 @@ export async function registration(
if (error) {
const errors: Record<string, string> = {}
errors['server'] = `-error-2 -> ${error}`
return errors;
return {
previousState: {
full_name,
email,
password,
confirm_password,
phone,
company
},
error: errors
};
}
throw error;
}