RegisterCustomer
Implements RegisterCustomer
Unified Method.
Source
/* eslint-disable etc/throw-error */
import { defineApi } from "@vsf-enterprise/unified-api-commercetools";
import { getNormalizers } from "@vsf-enterprise/unified-api-commercetools/udl";
export const registerCustomer = defineApi.registerCustomer(async (context, args) => {
const { firstName, lastName, email, password } = args;
if (!checkPasswordComplexity(password)) {
throw { statusCode: 422, message: "Password does not meet complexity requirements" };
}
try {
const response = await context.api.customerSignMeUp({
email,
password,
firstName,
lastName,
});
const { normalizeCustomer } = getNormalizers(context);
return {
customer: normalizeCustomer(response.user.customer),
};
} catch {
throw { statusCode: 400, message: "Could not register customer" };
}
});
function checkPasswordComplexity(password: string): boolean {
if (password.length < 8) {
return false;
}
if (!/\d/.test(password)) {
return false;
}
return !!/[A-Z]/.test(password);
}