Initializes the customer session by checking for a stored access token. If a valid token exists, fetches and returns the customer data. Call this on page load to restore logged-in state.
Returns
Promise<ShopifyCustomer | null> - Customer object if logged in, null otherwise.
Examples
Basic initialization
// On page load
const customer = await shoppy.customer.init()
if (customer) {
console.log(`Logged in as ${customer.email}`)
showAccountMenu()
} else {
showLoginForm()
}
With error handling
try {
const customer = await shoppy.customer.init()
if (customer) {
updateUI(customer)
}
} catch (error) {
// Token was invalid or expired
console.log('Session expired, please log in again')
}
Combined with subscribe
// Set up subscription first
shoppy.customer.subscribe((customer) => {
updateNavbar(customer)
})
// Then initialize - will trigger subscriber with restored state
await shoppy.customer.init()
Always call .init() early in your page lifecycle. This restores the customer session and triggers any subscribers with the current state.