feat: add auth middleware

This commit is contained in:
SauceyRed 2025-05-28 02:18:29 +02:00
parent 22ca450651
commit 6a11108ec1
Signed by: sauceyred
GPG key ID: 2BF92EB6D8A5CCA7

17
middleware/auth.global.ts Normal file
View file

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