'use server' import { SignupFormSchema, loginFormSchema, localDevelopmentUrl } from '@/app/actions/definitions'; import { createSession, deleteSession, getSessionData } from '@/app/actions/session'; import { redirect } from 'next/navigation'; const API_BASE_URL = process.env.NODE_ENV === 'development' ? localDevelopmentUrl : 'http://app:8080'; export async function logout() { const token = await getSessionData('token'); try { await fetch(`${API_BASE_URL}/v1/api/auth/logout`, { method: 'POST', headers: { "Content-Type": "application/json", "Accept": "application/json", "Authorization": `Bearer ${token}`, } }); } catch (error) { throw error; } await deleteSession(); redirect('/login'); } export async function authorization( state: { previousState: { email: string; } error: Record } | undefined, formData: FormData, ) { const email = formData.get('email') as string || ''; const password = formData.get('password'); /* для теста */ if (email === "test" && password === "test") { await createSession("1111", "test"); redirect('/pages/dashboard'); } /* для теста */ const validatedFields = loginFormSchema.safeParse({ email, password }) if (!validatedFields.success) { const errors: Record = {} 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 { const { email, password } = validatedFields.data; const response = await fetch(`${API_BASE_URL}/api/v1/data`, { method: 'POST', body: JSON.stringify({ version: 1, msg_id: 20001, message_body: { email: email, password: password } }), headers: { "Content-Type": "application/json", "Accept": "application/json" } }); if (response.ok) { let parsed = await response.json(); if (parsed.message_desc === 'Operation successful') { await createSession(parsed.message_body.token, email); } else { throw (`${parsed.message_code}`); } } else { throw ('request-ended-with-an-error'); } } catch (error) { const errors: Record = {} 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; } if (error) { return { previousState: { email, password }, error: errors }; } throw error; } redirect('/pages/dashboard'); } type FormState = { previousState: { full_name: string; email: string; password: string; confirm_password: string; phone: string; company: string; agree: string; } error: Record }; export async function registration( state: FormState | undefined, formData: FormData, ): Promise { 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 || ''; const validatedFields = SignupFormSchema.safeParse({ full_name, email, phone: phone.replace(/[-\(\)\s]/g, ''), password, confirm_password, agree, }); if (!validatedFields.success) { const errors: Record = {} 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: { full_name, email, password, confirm_password, phone, company, agree }, error: errors }; } try { const { full_name, email, password, phone } = validatedFields.data; const response = await fetch(`${API_BASE_URL}/api/v1/data`, { method: 'POST', body: JSON.stringify({ version: 1, msg_id: 20002, message_body: { fullName: full_name, email, phone: phone, password, companyName: company, } }), headers: { "Content-Type": "application/json", "Accept": "application/json" } }); if (response.ok) { let parsed = await response.json(); if (parsed.message_desc === 'Operation successful') { await createSession(parsed.message_body.token, email); } else { throw (`${parsed.message_code}`); } } else { throw (`${response.status}`); } } catch (error) { if (error) { const errors: Record = {}; switch (error) { case '1': errors['server'] = 'email-or-already-registered' break; default: errors['server'] = 'request-ended-with-an-error' break; } return { previousState: { full_name, email, password, confirm_password, phone, company, agree }, error: errors }; } throw error; } redirect('/pages/dashboard'); }