return functio after merge

This commit is contained in:
smanylov
2026-02-17 19:20:04 +07:00
parent 1f61ba7edd
commit f4a13e2a8e
2 changed files with 149 additions and 1 deletions
+148
View File
@@ -483,3 +483,151 @@ export async function resendConfirmEmail(userId: number) {
} }
} }
} }
export type FormStateConfirmPassword = {
previousState: {
email: string;
password: string;
confirm_password: string;
verifyToken: string;
},
error: Record<string, string> | 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<FormStateConfirmPassword | undefined> {
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<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,
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<string, string> = {};
if (error) {
return {
previousState: {
email,
password,
confirm_password,
verifyToken
},
error: errors
};
}
throw error;
}
redirect('/pages/dashboard');
}
+1 -1
View File
@@ -14,7 +14,7 @@ interface initMessageBody {
token?: string 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'); const token = await getSessionData('token');
if (!token) { if (!token) {
return; return;