Files
chad/client/app/middleware/01.auth.global.ts
opti1337 2ac88f1010
All checks were successful
Deploy / publish-web (push) Successful in 2m14s
user preferences
2025-12-25 22:53:12 +06:00

22 lines
463 B
TypeScript

import chadApi from '#shared/chad-api'
export default defineNuxtRouteMiddleware(async (to, from) => {
const { me, setMe } = useAuth()
if (!me.value) {
try {
setMe(await chadApi('/me', { method: 'GET' }))
return navigateTo({ name: 'Index' })
}
catch {
if (to.meta.auth !== 'guest') {
return navigateTo({ name: 'Login' })
}
}
}
if (me.value && to.meta.auth === 'guest') {
return navigateTo('/')
}
})