Vue Storefront is now Alokai! Learn More
RegisterCustomer

RegisterCustomer

Implements RegisterCustomer Unified Method.

Source

import { getNormalizers } from "@alokai/connect/integration-kit";
import { HttpStatusCode } from "@alokai/connect/middleware";

import { defineApi } from "@vsf-enterprise/unified-api-commercetools";

export const registerCustomer = defineApi.registerCustomer(async (context, args) => {
  const { email, firstName, lastName, password } = args;

  if (!checkPasswordComplexity(password)) {
    throw context.createHttpError({
      message: "Password does not meet complexity requirements",
      statusCode: HttpStatusCode.UNPROCESSABLE_ENTITY,
    });
  }

  try {
    const { api } = await context.getApiClient();
    const response = await api.customerSignMeUp({
      email,
      firstName,
      lastName,
      password,
    });
    const { normalizeCustomer } = getNormalizers(context);

    return {
      customer: normalizeCustomer(response.user.customer),
    };
  } catch {
    throw context.createHttpError({
      message: "Could not register customer",
      statusCode: HttpStatusCode.BAD_REQUEST,
    });
  }
});

function checkPasswordComplexity(password: string): boolean {
  if (password.length < 8) {
    return false;
  }
  if (!/\d/.test(password)) {
    return false;
  }
  return !!/[A-Z]/.test(password);
}