feat (auth): implement global authentication middleware

This commit is contained in:
Abizrh
2025-08-10 16:47:07 +07:00
parent 0515f8628f
commit 66db96b9b6
11 changed files with 80 additions and 36 deletions
+18
View File
@@ -0,0 +1,18 @@
export default defineNuxtRouteMiddleware((to) => {
const { $pinia } = useNuxtApp()
if (import.meta.client) {
const userStore = useUserStore($pinia)
console.log('currRole', userStore.userRole)
console.log('isAuth', userStore.isAuthenticated)
if (!userStore.isAuthenticated) {
return navigateTo('/auth/login')
}
const allowedRoles = to.meta.roles as string[] | undefined
if (allowedRoles && !allowedRoles.includes(userStore.userRole)) {
return navigateTo('/unauthorized')
}
}
})
-7
View File
@@ -1,7 +0,0 @@
export default defineNuxtRouteMiddleware((to, from) => {
// TODO: change this to actual api
const user = true
if (!user) {
return navigateTo('/auth/login')
}
})