Polarcraft/web/middleware/auth.js

18 lines
369 B
JavaScript
Raw Normal View History

2023-04-23 17:16:53 +02:00
export default defineNuxtRouteMiddleware(async (to) => {
if (process.server) return;
try {
const user = await $fetch('/api/auth/user')
useState('user', () => user)
2023-05-27 12:02:33 +02:00
if (to.meta.moderator && !user.role.moderator) return navigateTo('/')
2023-04-23 17:16:53 +02:00
} catch (err) {
console.log(err)
useState('user', () => null)
return navigateTo('/login')
}
})