add reset password
This commit is contained in:
@@ -341,4 +341,152 @@ export async function confirmEmail(
|
||||
console.log('confirmEmail');
|
||||
const emailConfirm = formData.get('emailConfirm') as string || '';
|
||||
console.log(emailConfirm);
|
||||
}
|
||||
|
||||
export async function resetPassword(
|
||||
state: any | undefined,
|
||||
formData: any,
|
||||
) {
|
||||
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 parsed.message_body;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
redirect(`/reset-password?email=${email}`);
|
||||
}
|
||||
|
||||
export type FormStateConfirmPassword = {
|
||||
previousState: {
|
||||
email: string;
|
||||
password: string;
|
||||
confirm_password: string;
|
||||
verifyToken: string;
|
||||
},
|
||||
error: Record<string, string> | null
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
console.log(validatedFields);
|
||||
|
||||
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,5 +1,5 @@
|
||||
import * as z from 'zod'
|
||||
import { createValidationSchema } from '@/app/lib/validation-config-parser';
|
||||
import { createValidationSchema, createValidationSchemaForPssword } from '@/app/lib/validation-config-parser';
|
||||
import validationConfig from '@/app/lib/validation-config.json';
|
||||
|
||||
export type FormState =
|
||||
@@ -38,7 +38,7 @@ const defaultConfig = validationConfig as ValidationConfig;
|
||||
let cachedSignupSchema: any = null;
|
||||
let schemaPromise: Promise<any> | null = null;
|
||||
|
||||
async function loadConfigFromBackend(accountType: 'b2b' | 'b2c'): Promise<any> {
|
||||
async function loadConfigFromBackend(accountType?: 'b2b' | 'b2c'): Promise<any> {
|
||||
try {
|
||||
const response = await fetch('/api/validation-config');
|
||||
if (!response.ok) throw new Error('Failed to load config');
|
||||
@@ -50,15 +50,19 @@ async function loadConfigFromBackend(accountType: 'b2b' | 'b2c'): Promise<any> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function getSignupFormSchema(accountType: 'b2b' | 'b2c') {
|
||||
export async function getSignupFormSchema(schemaType?: 'b2b' | 'b2c' | 'resetPassword') {
|
||||
if (cachedSignupSchema) {
|
||||
return cachedSignupSchema;
|
||||
}
|
||||
|
||||
if (!schemaPromise) {
|
||||
schemaPromise = (async () => {
|
||||
const config = await loadConfigFromBackend(accountType);
|
||||
cachedSignupSchema = createValidationSchema(config, accountType);
|
||||
const config = await loadConfigFromBackend();
|
||||
if (schemaType === 'resetPassword') {
|
||||
cachedSignupSchema = createValidationSchemaForPssword(config);
|
||||
} else {
|
||||
cachedSignupSchema = createValidationSchema(config, schemaType);
|
||||
}
|
||||
|
||||
return cachedSignupSchema;
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user