Files
no-copy-frontend/src/app/actions/auth.ts
T

235 lines
4.9 KiB
TypeScript
Raw Normal View History

2025-11-27 21:33:53 +07:00
'use server'
2025-12-09 16:35:47 +07:00
import { SignupFormSchema, loginFormSchema, localDevelopmentUrl } from '@/app/actions/definitions';
import { createSession, deleteSession, getSessionData } from '@/app/actions/session';
2025-12-02 17:46:12 +07:00
import { redirect } from 'next/navigation';
const API_BASE_URL = process.env.NODE_ENV === 'development'
2025-12-03 21:02:31 +07:00
? localDevelopmentUrl
2025-12-04 12:49:00 +07:00
: 'http://app:8080';
2025-11-27 21:33:53 +07:00
export async function logout() {
2025-12-02 17:11:53 +07:00
const token = await getSessionData('token');
2025-11-28 15:28:15 +07:00
try {
2025-12-05 16:25:23 +07:00
await fetch(`${API_BASE_URL}/v1/api/auth/logout`, {
2025-11-28 15:28:15 +07:00
method: 'POST',
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": `Bearer ${token}`,
}
});
} catch (error) {
throw error;
}
await deleteSession();
redirect('/login');
2025-11-27 21:33:53 +07:00
}
export async function authorization(
2025-12-07 12:53:56 +07:00
state: {
previousState: {
email: string;
}
error: Record<string, string>
} | undefined,
2025-11-27 21:33:53 +07:00
formData: FormData,
) {
2025-12-07 12:53:56 +07:00
const email = formData.get('email') as string || '';
const password = formData.get('password');
2025-12-11 18:28:35 +07:00
/* для теста */
if (email === "test" && password === "test") {
await createSession("1111", "test");
redirect('/pages/dashboard');
}
/* для теста */
2025-11-28 15:28:15 +07:00
const validatedFields = loginFormSchema.safeParse({
2025-12-07 12:53:56 +07:00
email,
password
2025-11-28 15:28:15 +07:00
})
if (!validatedFields.success) {
2025-12-07 12:53:56 +07:00
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
};
2025-11-28 15:28:15 +07:00
}
2025-11-27 21:33:53 +07:00
try {
2025-11-28 15:28:15 +07:00
const { email, password } = validatedFields.data;
2025-12-12 14:29:00 +07:00
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
2025-11-28 15:28:15 +07:00
method: 'POST',
body: JSON.stringify({
2025-12-12 14:29:00 +07:00
version: 1,
msg_id: 20001,
message_body: {
email: email,
password: password
}
2025-11-28 15:28:15 +07:00
}),
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
});
if (response.ok) {
let parsed = await response.json();
2025-12-12 14:29:00 +07:00
await createSession(parsed.message_body.token, email);
2025-11-28 15:28:15 +07:00
} else {
2025-12-10 16:39:13 +07:00
throw ('request-ended-with-an-error');
2025-11-28 15:28:15 +07:00
}
2025-11-27 21:33:53 +07:00
} catch (error) {
2025-12-07 12:53:56 +07:00
const errors: Record<string, string> = {}
errors['server'] = `${error}`;
2025-11-27 21:33:53 +07:00
if (error) {
2025-12-07 12:53:56 +07:00
return {
previousState: {
email,
password
},
error: errors
};
2025-11-27 21:33:53 +07:00
}
throw error;
}
2025-11-28 15:28:15 +07:00
redirect('/pages/dashboard');
2025-11-27 21:33:53 +07:00
}
2025-12-07 12:53:56 +07:00
type FormState = {
previousState: {
full_name: string;
email: string;
password: string;
confirm_password: string;
phone: string;
company: string;
agree: string;
2025-12-07 12:53:56 +07:00
}
error: Record<string, string>
};
2025-11-27 21:33:53 +07:00
export async function registration(
2025-12-07 12:53:56 +07:00
state: FormState | undefined,
2025-11-27 21:33:53 +07:00
formData: FormData,
2025-12-07 12:53:56 +07:00
): 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 agree = formData.get('agree') as string || '';
2025-12-07 12:53:56 +07:00
2025-11-27 21:33:53 +07:00
const validatedFields = SignupFormSchema.safeParse({
2025-12-07 12:53:56 +07:00
full_name,
email,
phone: phone.replace(/[-\(\)\s]/g, ''),
2025-12-07 12:53:56 +07:00
password,
confirm_password,
agree,
2025-12-07 12:53:56 +07:00
});
2025-11-27 21:33:53 +07:00
if (!validatedFields.success) {
2025-11-28 12:38:14 +07:00
const errors: Record<string, string> = {}
JSON.parse(validatedFields.error.message).forEach((obj: {
message: string,
path: string
}) => {
if (errors[obj.path[0]]) {
2025-12-10 16:39:13 +07:00
errors[obj.path[0]] = `${errors[obj.path[0]]}&${obj.message}`;
2025-11-28 12:38:14 +07:00
} else {
errors[obj.path[0]] = obj.message;
}
});
2025-12-07 12:53:56 +07:00
return {
previousState: {
full_name,
email,
password,
confirm_password,
phone,
company,
agree
2025-12-07 12:53:56 +07:00
},
error: errors
};
2025-11-27 21:33:53 +07:00
}
try {
const { full_name, email, password, phone } = validatedFields.data;
2025-12-12 14:29:00 +07:00
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
2025-11-27 21:33:53 +07:00
method: 'POST',
2025-12-11 18:28:35 +07:00
body: JSON.stringify({
2025-12-12 14:29:00 +07:00
version: 1,
msg_id: 20002,
message_body: {
fullName: full_name,
email,
phone: phone,
password,
companyName: company,
}
2025-12-11 18:28:35 +07:00
}),
2025-11-27 21:33:53 +07:00
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
});
2025-11-28 15:28:15 +07:00
2025-12-12 11:52:57 +07:00
console.log('--response--');
console.log(response);
console.log('--response--');
2025-11-28 15:28:15 +07:00
if (response.ok) {
let parsed = await response.json();
2025-12-11 18:28:35 +07:00
console.log('--parsed--');
console.log(parsed);
2025-12-12 14:29:00 +07:00
await createSession(parsed.message_body.token, email);
2025-11-28 15:28:15 +07:00
} else {
throw (`${response.status}`);
2025-11-28 15:28:15 +07:00
}
2025-11-27 21:33:53 +07:00
} catch (error) {
if (error) {
const errors: Record<string, string> = {};
2025-12-10 16:39:13 +07:00
errors['server'] = 'request-ended-with-an-error'
2025-12-07 12:53:56 +07:00
return {
previousState: {
full_name,
email,
password,
confirm_password,
phone,
company,
agree
2025-12-07 12:53:56 +07:00
},
error: errors
};
2025-11-27 21:33:53 +07:00
}
throw error;
}
redirect('/pages/dashboard');
}