Vue Storefront is now Alokai! Learn More
LogoutCustomer

LogoutCustomer

Implements LogoutCustomer Unified Method.

Source

import "./extended";
import { ANONYMOUS_USER } from "@vsf-enterprise/sapcc-api";

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

export const logoutCustomer = defineApi.logoutCustomer(async (context) => {
  const authUserIdCookieName = context.config.headersNames!.authUserIdCookie!;

  try {
    if (context.req.cookies[authUserIdCookieName]) {
      // emulation ended but ASM agent is still logged in, he's token cannot be cleared
      // as his session continues and should act as an anonymous user again
      context.res.cookie(authUserIdCookieName, ANONYMOUS_USER);
    } else {
      // ASM agent is not logged in, so the customer is logged in and his token must be cleared
      await context.extendedApi.auth.signUserOut();
    }
    context.res.clearCookie("unified-cart-id");
    // biome-ignore lint/suspicious/noEmptyBlockStatements: silent error handling
  } catch {}
});