Files
chad/client/app/middleware/01.auth.global.ts
opti1337 bf38267c37
All checks were successful
Deploy / deploy (push) Successful in 36s
user preferences
2025-12-25 22:50:56 +06:00

21 lines
420 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' }))
}
catch {
if (to.meta.auth !== 'guest') {
return navigateTo({ name: 'Login' })
}
}
}
if (me.value && to.meta.auth === 'guest') {
return navigateTo('/')
}
})