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>
16 lines
725 B
TypeScript
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);
|
|
});
|