ChangeCustomerPassword
Implements ChangeCustomerPassword Unified Method.
Source
import { validatePassword } from "@alokai/connect/integration-kit";
import { HttpError, HttpStatusCode } from "@alokai/connect/middleware";
import { defineApi, getCurrentCustomer } from "@vsf-enterprise/unified-api-bigcommerce";
const isUnauthorizedError = (error: unknown) =>
error instanceof HttpError && error.statusCode === HttpStatusCode.FORBIDDEN;
export const changeCustomerPassword = defineApi.changeCustomerPassword(async (context, args) => {
const { email } = await getCurrentCustomer(context);
const { confirmPassword, currentPassword, newPassword } = args;
if (!validatePassword(newPassword, confirmPassword)) {
throw context.createHttpError({
message: "Password does not meet the requirements",
statusCode: HttpStatusCode.UNPROCESSABLE_ENTITY,
});
}
try {
const { api } = await context.getApiClient();
await api.updateCustomer({
authentication: {
new_password: newPassword,
},
validation: {
email: email!,
password: currentPassword,
},
});
} catch (error) {
if (isUnauthorizedError(error)) {
throw context.createHttpError({
message: "Password change failed",
statusCode: HttpStatusCode.FORBIDDEN,
});
}
throw error;
}
});