Vue Storefront is now Alokai! Learn More
LogoutCustomer

LogoutCustomer

Implements LogoutCustomer Unified Method.

Source

import { ANONYMOUS_USER, AUTH_USER_ID_COOKIE_NAME } from "@vsf-enterprise/sapcc-api";
import { defineApi } from "@vsf-enterprise/unified-api-sapcc";
import from "@vsf-enterprise/unified-api-sapcc/udl";

declare module "@vsf-enterprise/unified-api-sapcc" {
  interface LogoutCustomerExtendedArgs {}
}


export const logoutCustomer = defineApi.logoutCustomer(async (context) => {
  try {
    if (context.req.cookies[AUTH_USER_ID_COOKIE_NAME]) {
      // 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(AUTH_USER_ID_COOKIE_NAME, 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 {}
});