Polarcraft/web/middleware/auth.js

18 lines
356 B
JavaScript

export default defineNuxtRouteMiddleware(async (to) => {
if (process.server) return;
try {
const user = await $fetch('/api/auth/user')
useState('user', () => user)
if (to.meta.admin && !user.admin) return navigateTo('/')
} catch (err) {
console.log(err)
useState('user', () => null)
return navigateTo('/login')
}
})