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

339 lines
7.1 KiB
TypeScript
Raw Normal View History

2025-11-27 21:33:53 +07:00
'use server'
2026-01-12 14:16:34 +07:00
import { loginFormSchema, API_BASE_URL, getSignupFormSchema } from '@/app/actions/definitions';
2026-01-14 18:15:52 +07:00
import { createSession, deleteSession, getSessionData, updateSession } from '@/app/actions/session';
2025-12-02 17:46:12 +07:00
import { redirect } from 'next/navigation';
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: {
2026-01-28 18:53:08 +07:00
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${token}`,
2025-11-28 15:28:15 +07:00
}
});
} catch (error) {
throw error;
2026-01-12 13:39:35 +07:00
} finally {
await deleteSession();
2025-11-28 15:28:15 +07:00
}
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
/* для теста */
2026-01-28 18:53:08 +07:00
if (email === 'test' && password === 'test') {
await createSession('1111', 'test');
2025-12-11 18:28:35 +07:00
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: {
2026-01-28 18:53:08 +07:00
'Content-Type': 'application/json',
'Accept': 'application/json'
2025-11-28 15:28:15 +07:00
}
});
if (response.ok) {
let parsed = await response.json();
2025-12-17 14:30:54 +07:00
if (parsed.message_desc === 'Operation successful') {
await createSession(parsed.message_body.token, email);
} else {
throw (`${parsed.message_code}`);
}
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> = {}
2025-12-17 14:30:54 +07:00
switch (error) {
case '2':
errors['server'] = 'password-does-not-match'
break;
case '4':
errors['server'] = 'email-not-found'
break;
default:
errors['server'] = 'request-ended-with-an-error'
break;
}
2025-12-07 12:53:56 +07:00
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-22 13:02:39 +07:00
export type FormState = {
2025-12-07 12:53:56 +07:00
previousState: {
2025-12-19 16:11:58 +07:00
fullName: string;
2025-12-07 12:53:56 +07:00
email: string;
password: string;
confirm_password: string;
phone: string;
2026-01-29 14:34:04 +07:00
companyName?: string;
agree: string;
2026-01-14 18:15:52 +07:00
},
mailConfirm?: boolean,
error: Record<string, string> | null
2025-12-07 12:53:56 +07:00
};
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> {
2026-01-29 21:34:02 +07:00
const accountType = formData.get('accountType') as 'b2b' | 'b2c';
2026-01-29 14:34:04 +07:00
2025-12-19 16:11:58 +07:00
const fullName = formData.get('fullName') as string || '';
2025-12-07 12:53:56 +07:00
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 || '';
2025-12-19 16:11:58 +07:00
const companyName = formData.get('companyName') as string || '';
const agree = formData.get('agree') as string || '';
2025-12-07 12:53:56 +07:00
2026-01-29 21:34:02 +07:00
const SignupFormSchema = await getSignupFormSchema(accountType);
2026-01-10 15:12:51 +07:00
const dataToValidate = {
2025-12-19 16:11:58 +07:00
fullName,
2025-12-07 12:53:56 +07:00
email,
phone: phone.replace(/[-\(\)\s]/g, ''),
2025-12-07 12:53:56 +07:00
password,
confirm_password,
agree,
2026-01-29 21:34:02 +07:00
...(accountType === 'b2b' && { companyName })
2026-01-10 15:12:51 +07:00
};
const validatedFields = SignupFormSchema.safeParse(dataToValidate);
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: {
2025-12-19 16:11:58 +07:00
fullName,
2025-12-07 12:53:56 +07:00
email,
password,
confirm_password,
phone,
2026-01-29 14:34:04 +07:00
agree,
2026-01-29 21:34:02 +07:00
...(accountType === 'b2b' && { companyName })
2025-12-07 12:53:56 +07:00
},
error: errors
};
2025-11-27 21:33:53 +07:00
}
try {
2025-12-19 16:11:58 +07:00
const { fullName, 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: {
2025-12-19 16:11:58 +07:00
fullName: fullName,
2025-12-12 14:29:00 +07:00
email,
phone: phone,
password,
2026-01-29 17:01:00 +07:00
accountType,
2026-01-29 21:34:02 +07:00
...(accountType === 'b2b' && { companyName: companyName })
2025-12-12 14:29:00 +07:00
}
2025-12-11 18:28:35 +07:00
}),
2025-11-27 21:33:53 +07:00
headers: {
2026-01-28 18:53:08 +07:00
'Content-Type': 'application/json',
'Accept': 'application/json'
2025-11-27 21:33:53 +07:00
}
});
2025-11-28 15:28:15 +07:00
if (response.ok) {
2025-12-19 16:11:58 +07:00
let parsed = await response.json() as {
message_desc: string,
message_body: { token: string },
message_code: string
};
2026-01-29 21:34:02 +07:00
2025-12-17 14:30:54 +07:00
if (parsed.message_desc === 'Operation successful') {
2026-01-10 15:12:51 +07:00
await createSession(parsed.message_body.token, email as string);
2025-12-17 14:30:54 +07:00
} else {
2025-12-19 16:11:58 +07:00
throw parsed;
2025-12-17 14:30:54 +07:00
}
2025-11-28 15:28:15 +07:00
} else {
throw (`${response.status}`);
2025-11-28 15:28:15 +07:00
}
2026-01-14 18:17:02 +07:00
/* return {
2026-01-14 18:15:52 +07:00
previousState: {
fullName,
email,
password,
confirm_password,
phone,
companyName,
agree
},
mailConfirm: true,
error: null
2026-01-14 18:17:02 +07:00
}; */
2025-12-19 16:11:58 +07:00
} catch (error: unknown) {
2025-11-27 21:33:53 +07:00
if (error) {
2025-12-19 16:11:58 +07:00
const typedError = error as any;
const errors: Record<string, string> = {};
2025-12-19 16:11:58 +07:00
switch (typedError.message_code) {
case 1:
2025-12-18 13:36:56 +07:00
errors['server'] = 'email-or-already-registered'
2025-12-17 14:30:54 +07:00
break;
2025-12-19 16:11:58 +07:00
case 2:
typedError.message_body.fieldErrors.forEach((obj: {
code: string,
field: string
}) => {
if (errors[obj.field]) {
errors[obj.field] = `${errors[obj.field]}&${obj.code.replaceAll('.', '-')}`;
} else {
errors[obj.field] = obj.code.replaceAll('.', '-');
}
});
break;
2025-12-17 14:30:54 +07:00
default:
errors['server'] = 'request-ended-with-an-error'
break;
}
2025-12-07 12:53:56 +07:00
return {
previousState: {
2025-12-19 16:11:58 +07:00
fullName,
2025-12-07 12:53:56 +07:00
email,
password,
confirm_password,
phone,
2025-12-19 16:11:58 +07:00
companyName,
agree
2025-12-07 12:53:56 +07:00
},
error: errors
};
2025-11-27 21:33:53 +07:00
}
2025-12-26 14:03:57 +07:00
2025-11-27 21:33:53 +07:00
throw error;
}
2026-01-14 18:17:02 +07:00
redirect('/pages/dashboard');
2026-01-14 18:15:52 +07:00
/* console.log('/v2');
redirect('/v2'); */
2026-01-12 13:39:35 +07:00
}
export async function tokenLifeExtension() {
const token = await getSessionData('token');
try {
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
method: 'POST',
headers: {
2026-01-28 18:53:08 +07:00
'Content-Type': 'application/json',
'Accept': 'application/json',
2026-01-12 13:39:35 +07:00
},
body: JSON.stringify({
version: 1,
msg_id: 20008,
message_body: {
token: token
}
}),
});
if (response.ok) {
const parsed = await response.json();
2026-01-20 16:53:11 +07:00
if (parsed.message_code === 0) {
await updateSession(parsed.message_body.token);
}
2026-01-12 13:39:35 +07:00
}
} catch (error) {
await deleteSession();
throw error;
}
2026-01-14 18:15:52 +07:00
}
export async function confirmEmail(
state: any | undefined,
formData: any,
) {
console.log('confirmEmail');
const emailConfirm = formData.get('emailConfirm') as string || '';
console.log(emailConfirm);
2025-11-27 21:33:53 +07:00
}