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;
});| Parameter | Type | Required | Description |
|---|---|---|---|
| Nil | Nil | Nil | This 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
- Re-read live variables: Inside the listener, read
Superfans.variables.customerandSuperfans.variables.storeagain. Do not use cached objects from page load. - Refresh
store.accessToken: Login and logout both replace the storefront token. Capture it fromstore.accessTokenin this callback before making Storefront requests. - Re-render personalized sections: Refresh loyalty, wishlist, or order history after login.
- Clear sensitive data: On logout, reset local storage or cached user data, and stop using the previous token.
Caveats
- Immediate firing: This event fires when login state changes, not on page load.
- No replay: If you subscribe after login, you will not receive the last known state.
- No token argument: The callback does not receive
accessToken. ReadSuperfans.variables.store.accessTokenafter the merge.
Updated about 1 month ago
Did this page help you?