Vue Storefront is now Alokai! Learn More
LoginCustomer

LoginCustomer

Implements LoginCustomer Unified Method.

Source

/* eslint-disable max-statements */
import { defineApi } from "@vsf-enterprise/unified-api-sfcc";
import type { SFCCIntegrationContext } from "@vsf-enterprise/sfcc-api";
import type { Customer } from "@vsf-enterprise/sfcc-types";
import { getNormalizers } from "@vue-storefront/unified-data-model";
import "./extended.d";

const MESSAGE_LOGIN_ERROR = "Could not login customer";
const MESSAGE_ALREADY_LOGGED_IN = "Customer is already logged in";

export const loginCustomer = defineApi.loginCustomer(async (context, args) => {
  const { email, password } = args;
  const { normalizeCustomer } = getNormalizers(context);
  let customerData: Customer;
  const basket = await context.api.getBasket();

  if (await isAlreadyLoggedIn(context)) {
    throw { statusCode: 403, message: MESSAGE_ALREADY_LOGGED_IN };
  }

  try {
    customerData = await context.api.signIn({
      username: email,
      password: password,
    });
  } catch (error) {
    // eslint-disable-next-line etc/throw-error
    throw { statusCode: 401, message: MESSAGE_LOGIN_ERROR, extra: error };
  }

  if (basket && basket.productItems?.length) {
    await context.api.setBasketCustomerInfo({
      basketId: basket.basketId,
      email: customerData.email!,
    });
  }

  return { customer: normalizeCustomer(customerData) };
});

async function isAlreadyLoggedIn(context: SFCCIntegrationContext) {
  try {
    const customer = await context.api.getCustomer();
    return !!customer.customerId;
  } catch {
    return false;
  }
}