Overview
Lunar separates authentication (handled by Laravel) from customer data (stored in Lunar’sCustomer model). The two are linked through the LunarUser trait, which adds customer relationships to the application’s User model. This guide walks through setting up the connection, managing the storefront session, and handling cart persistence across login and logout.
The examples below use standard Laravel controllers and Blade templates. The same concepts apply whether the storefront is built with Livewire, Inertia, or a headless API.
Setting Up the User Model
Add theLunarUser trait to the application’s User model. This adds relationships to customers, carts, and orders.
LunarUser trait provides the following relationships:
It also provides a helper method:
A user can be associated with multiple customers. This supports scenarios like a sales representative managing multiple accounts. For most storefronts, each user has a single customer record.
Creating a Customer on Registration
When a new user registers, create a correspondingLunar\Models\Customer record and link the two together.
Registration Controller
Adding to an Existing Registration Flow
If the application already has registration (for example, via Laravel Breeze or Fortify), add customer creation using a listener on theRegistered event:
EventServiceProvider or using the Event facade:
The Storefront Session
TheStorefrontSession facade manages the current customer, channel, currency, and customer groups for the session. It initializes automatically and resolves the customer from the authenticated user.
How Customer Resolution Works
When theStorefrontSession initializes, it resolves the current customer using this logic:
- Check the session for a previously stored customer ID
- If none found and a user is authenticated (with the
LunarUsertrait), call$user->latestCustomer()to find the most recent customer - Store the resolved customer ID in the session for subsequent requests
Setting the Customer Manually
In some cases, the customer needs to be set explicitly, for example when a user has multiple customer accounts:Changing Channel or Currency
Cart Behavior on Login and Logout
Lunar automatically handles cart persistence when users log in and out through theCartSessionAuthListener. This listener is registered by Lunar’s service provider and responds to Laravel’s Login and Logout authentication events.
What Happens on Login
When a user logs in, the listener follows this logic:- If a guest cart exists in the session (no
user_id), it is associated with the user. Depending on the configured policy, the guest cart items are either merged with the user’s existing cart or override it. - If no guest cart exists, the listener looks for the user’s most recent active cart and restores it to the session.
What Happens on Logout
When a user logs out, the cart session is cleared. The cart remains in the database and will be restored on the next login.Cart Association Policy
The association policy is configured inconfig/lunar/cart.php:
Customer Account Page
Build an account dashboard that displays the customer’s profile, linked addresses, and recent orders.Account Controller
Displaying Customer Details
Updating Customer Profile
Checking Authentication in Views
Use theStorefrontSession to conditionally display content based on whether a customer is linked:
Routes
Putting It All Together
Here is a complete registration controller and account controller:Next Steps
- Review the Customers reference for the full list of customer model fields, relationships, and scopes.
- Review the Storefront Session reference for all session management methods.
- Review the Customer Addresses guide for managing saved addresses.
- Review the Order History guide for displaying past orders.
- Review the Cart guide for details on how cart calculation and session management work.