From f4a13e2a8ed908eab125dee9452ffdd581776f7b Mon Sep 17 00:00:00 2001 From: smanylov Date: Tue, 17 Feb 2026 19:20:04 +0700 Subject: [PATCH] return functio after merge --- src/app/actions/auth.ts | 148 ++++++++++++++++++++++++++++++++++ src/app/actions/fileUpload.ts | 2 +- 2 files changed, 149 insertions(+), 1 deletion(-) diff --git a/src/app/actions/auth.ts b/src/app/actions/auth.ts index 823d3ac..4551734 100644 --- a/src/app/actions/auth.ts +++ b/src/app/actions/auth.ts @@ -482,4 +482,152 @@ export async function resendConfirmEmail(userId: number) { fine: false } } +} + +export type FormStateConfirmPassword = { + previousState: { + email: string; + password: string; + confirm_password: string; + verifyToken: string; + }, + error: Record | null +}; + +export async function resetPassword( + state: any | undefined, + formData: FormData, +) { + const email = formData.get('email'); + console.log('resetPassword'); + + try { + const response = await fetch(`${API_BASE_URL}/api/v1/data`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }, + body: JSON.stringify({ + version: 1, + msg_id: 20010, + message_body: { + email: email, + action: "resetPassword" + } + }), + }); + + if (response.ok) { + const parsed = await response.json(); + console.log(parsed); + if (parsed.message_code === 0) { + + } else { + throw 'error'; + } + } + } catch (error) { + return { + error: error + }; + } + console.log('redirect') + redirect(`/reset-password?email=${email}`); +} + + +export async function confirmPassword( + state: FormStateConfirmPassword | undefined, + formData: any, +): Promise { + const email = formData.get('email'); + const password = formData.get('password'); + const confirm_password = formData.get('confirm_password'); + const verifyToken = formData.get('verify-token'); + console.log('confirmPassword'); + + const SignupFormSchema = await getSignupFormSchema('resetPassword'); + + const dataToValidate = { + password, + confirm_password + }; + + const validatedFields = SignupFormSchema.safeParse(dataToValidate); + + 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, + password, + confirm_password, + verifyToken + }, + error: errors + }; + } + + try { + const response = await fetch(`${API_BASE_URL}/api/v1/data`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }, + body: JSON.stringify({ + version: 1, + msg_id: 20010, + message_body: { + email: email, + password: password, + verifyToken: verifyToken, + action: "confirmVerification" + } + }), + }); + + if (response.ok) { + + const parsed = await response.json(); + console.log(parsed); + if (parsed.message_code === 0) { + await createSession(parsed.message_body.authToken, email as string); + } else { + throw parsed.message_body; + } + } else { + throw (`${response.status}`); + } + } catch (error: unknown) { + const typedError = error as any; + const errors: Record = {}; + + if (error) { + return { + previousState: { + email, + password, + confirm_password, + verifyToken + }, + error: errors + }; + } + throw error; + } + + redirect('/pages/dashboard'); } \ No newline at end of file diff --git a/src/app/actions/fileUpload.ts b/src/app/actions/fileUpload.ts index 776e1c4..31cb374 100644 --- a/src/app/actions/fileUpload.ts +++ b/src/app/actions/fileUpload.ts @@ -14,7 +14,7 @@ interface initMessageBody { token?: string } -export async function fileUpload(messageBody: initMessageBody, convertTo: string | null | undefined) { +export async function fileUpload(messageBody: initMessageBody, convertTo?: string | null | undefined) { const token = await getSessionData('token'); if (!token) { return;