feat: add auth middleware
This commit is contained in:
parent
22ca450651
commit
6a11108ec1
1 changed files with 17 additions and 0 deletions
17
middleware/auth.global.ts
Normal file
17
middleware/auth.global.ts
Normal 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");
|
||||
}
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue