Middlewares
User Guest Checkout
๐ Global Middleware โ user-guest-checkout.global.ts
This middleware manages guest checkout sessions and ensures proper cleanup or transition when a guest user navigates outside the intended checkout flow.
๐ Purpose
- Monitors user navigation to maintain or clear guest checkout state.
- Protects against accidental abandonment of guest sessions.
- Ensures trolleys are cleared when needed.
- Prevents lingering guest state after exiting the checkout.
๐ง Logic Breakdown
export default defineNuxtRouteMiddleware(async (to, from) => {
- Runs globally on every route change.
๐ซ Exit Early for Protected Page
if (to.path === "/protected") return;
Skips processing for /protected route.
๐ฆ Guest Checkout State Handling
const { useAuthStore } = await import("~/stores/auth.store");
const authStore = useAuthStore();
Loads the authStore dynamically to access guest-related flags.
๐ก If Navigating Outside Checkout or Order Confirmation
if (to.meta.layout !== "checkout" && to.meta.layout !== "order-confirmation") {
authStore.is_guest_checkout = false;
- Ends guest checkout if the user leaves the checkout or confirmation layout.
๐ Confirm Session Cancellation
if (authStore.guest) {
const isGuestCheckoutSessionOver =
from.meta.layout === "order-confirmation" ||
window.confirm("Weet u zeker dat u het afrekenen wilt annuleren?...");
- Prompts the user to confirm session abandonment unless coming from confirmation.
๐งน Clean Up Guest Session
if (isGuestCheckoutSessionOver) {
authStore.guest = null;
const { useTrolleyStore } = await import("~/stores/trolley.store");
const trolleyStore = useTrolleyStore();
trolleyStore.destroyTrolley();
- Clears guest and trolley data if confirmed.
๐ซ Cancel Navigation
} else {
return abortNavigation();
- If user cancels, block navigation.
๐ค Regular User Fallback
if (to.meta.layout !== "auth" && !authStore.is_authenticated && !authStore.is_guest_checkout) {
const { useRootStore } = await import("~/stores/root.store");
const rootStore = useRootStore();
rootStore.after_auth_intended_route = null;
- Clears intended auth redirect for anonymous users outside auth context.
โ
When It Runs
- On every route navigation unless it's to
/protected
๐ก Use Cases
- Guest user tries to navigate away from checkout
- Protect trolleys from premature clearing
- Reset guest state cleanly outside of checkout flow
๐ Notes
- Uses dynamic imports for stores (performance optimization)
- UI confirmation uses native
window.confirm - Relies on
meta.layoutto differentiate page types