✨ feat (auth): implement global authentication middleware
This commit is contained in:
@@ -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')
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -1,7 +0,0 @@
|
||||
export default defineNuxtRouteMiddleware((to, from) => {
|
||||
// TODO: change this to actual api
|
||||
const user = true
|
||||
if (!user) {
|
||||
return navigateTo('/auth/login')
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user