import { signInPath } from "~/utils/auth"; export default defineNuxtRouteMiddleware(async (to) => { if (import.meta.server) return; // The requested page, not the current one, is where the shopper should land // after signing in — deep links into the buyer centre must survive. const signIn = signInPath(to.fullPath); const session = useSessionStore(); if (!session.token) session.hydrate(); if (!session.token) return navigateTo(signIn); // Trust the auth API's answer rather than whatever localStorage claims; a // rejected token clears the session inside validate(). if (!(await session.validate())) return navigateTo(signIn); if (session.user?.role !== "customer") return navigateTo(signIn); });