From 6a11108ec15a7eba5af10c2570797c0b10b6c080 Mon Sep 17 00:00:00 2001 From: SauceyRed Date: Wed, 28 May 2025 02:18:29 +0200 Subject: [PATCH] feat: add auth middleware --- middleware/auth.global.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 middleware/auth.global.ts diff --git a/middleware/auth.global.ts b/middleware/auth.global.ts new file mode 100644 index 0000000..e567c70 --- /dev/null +++ b/middleware/auth.global.ts @@ -0,0 +1,17 @@ +export default defineNuxtRouteMiddleware(async (to, from) => { + console.log("to.path:", to.path); + const accessToken = useCookie("access_token").value; + if (["/login", "/register"].includes(to.path)) { + if (accessToken) { + return await navigateTo("/"); + } + return; + }; + + if (!accessToken) { + const { refresh } = useAuth(); + console.log("hi"); + await refresh(); + return await navigateTo("/login"); + } +})