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.layout to differentiate page types


Copyright ยฉ 2026