The Vue Storefront Essentials Course is now available! Learn More
RegisterCustomer

RegisterCustomer

Implements RegisterCustomer Unified Method.

Source

import { getNormalizers } from "@vue-storefront/unified-data-model";
import { defineApi } from "@vsf-enterprise/unified-api-sapcc";
import { AUTH_USER_TOKEN_COOKIE_NAME } from "@vsf-enterprise/sapcc-api";

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

  const user = await context.api.createUser({
    firstName: firstName,
    lastName: lastName,
    uid: email,
    password: password,
  });

  const loginData = await context.api.OAuthUserAuthorization({
    username: email,
    password: password,
  });
  context.req.cookies[AUTH_USER_TOKEN_COOKIE_NAME] = JSON.stringify(loginData.token);

  return {
    customer: normalizeCustomer(user),
  };
});