mirror of
https://github.com/OpenSquawk/OpenSquawk
synced 2026-05-15 03:25:40 +08:00
22 lines
563 B
TypeScript
22 lines
563 B
TypeScript
import { defineNuxtRouteMiddleware, navigateTo } from '#app'
|
|
import { useAuthStore } from '~/stores/auth'
|
|
|
|
export default defineNuxtRouteMiddleware(async (to) => {
|
|
const auth = useAuthStore()
|
|
|
|
if (!auth.accessToken) {
|
|
await auth.tryRefresh().catch(() => false)
|
|
}
|
|
|
|
if (!auth.initialized) {
|
|
await auth.fetchUser().catch(() => null)
|
|
} else if (!auth.user) {
|
|
await auth.fetchUser().catch(() => null)
|
|
}
|
|
|
|
if (!auth.user) {
|
|
const redirect = encodeURIComponent(to.fullPath || '/')
|
|
return navigateTo(`/login?redirect=${redirect}`)
|
|
}
|
|
})
|