Login Status Change

An event that keeps an eye if customer has logged in or not

Description

The onLoginStatusChanged event triggers whenever a customer logs in or logs out of their account.
It helps you personalize the experience — for example, showing customer-specific data, loyalty points, or updated navigation.

Native merges the new app context into Superfans.variables before your callback runs. That update includes customer and a refreshed store.accessToken. Re-read both inside the listener. Do not keep a token from before login or logout.

See Store for the store variable.

Function Signature

Superfans.listeners.onLoginStatusChanged(() => {
  const customer = Superfans.variables.customer;
  const store = Superfans.variables.store;
  const accessToken = store?.accessToken;
});
ParameterTypeRequiredDescription
NilNilNilThis listener does not require any parameter.

Example Usage

Superfans.listeners.onLoginStatusChanged(() => {
  const customer = Superfans.variables.customer;
  const store = Superfans.variables.store;
  const accessToken = store?.accessToken;

  if (customer?.id) {
    const customerName = customer.firstName;
    console.log(`Welcome back, ${customerName}`);
    // Use accessToken for any storefront request that must run as the logged-in customer.
  } else {
    console.log("Logged out");
    // Use the updated accessToken. Do not reuse the pre-logout token.
  }
});

Best Practices

  1. Re-read live variables: Inside the listener, read Superfans.variables.customer and Superfans.variables.store again. Do not use cached objects from page load.
  2. Refresh store.accessToken: Login and logout both replace the storefront token. Capture it from store.accessToken in this callback before making Storefront requests.
  3. Re-render personalized sections: Refresh loyalty, wishlist, or order history after login.
  4. Clear sensitive data: On logout, reset local storage or cached user data, and stop using the previous token.

Caveats

  1. Immediate firing: This event fires when login state changes, not on page load.
  2. No replay: If you subscribe after login, you will not receive the last known state.
  3. No token argument: The callback does not receive accessToken. Read Superfans.variables.store.accessToken after the merge.

Did this page help you?