mirror of
https://github.com/OpenSquawk/OpenSquawk
synced 2026-05-20 07:05:44 +08:00
20 lines
501 B
TypeScript
20 lines
501 B
TypeScript
import { defineNuxtRouteMiddleware, navigateTo } from '#app'
|
|
import { useAuthStore } from '~/stores/auth'
|
|
|
|
export default defineNuxtRouteMiddleware(async (to) => {
|
|
const auth = useAuthStore()
|
|
|
|
if (!auth.initialized) {
|
|
await auth.fetchUser().catch(() => null)
|
|
}
|
|
|
|
if (!auth.user) {
|
|
const redirect = encodeURIComponent(to.fullPath || '/admin')
|
|
return navigateTo(`/login?redirect=${redirect}`)
|
|
}
|
|
|
|
if (!['admin', 'dev'].includes(auth.user.role)) {
|
|
return navigateTo('/')
|
|
}
|
|
})
|