Files
vmall/apps/mall/middleware/auth.ts
T
Chengdong ZhangandCursor 6c1357ec4d feat: persist customer product and shop favorites through the live API
Replace mall fixture favorites with customer-scoped endpoints, and send signed-out shoppers back to the page they left after sign-in.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-21 18:51:45 +08:00

16 lines
725 B
TypeScript

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);
});