Polarcraft/web/middleware/auth.js
Xeovalyte 8121b9b975
Some checks failed
Build and Deploy / Deploy Web (push) Has been cancelled
Build and Deploy / Deploy Discord Bot (push) Has been cancelled
Lots of improvements and changes
2023-05-27 12:02:33 +02:00

18 lines
369 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.moderator && !user.role.moderator) return navigateTo('/')
} catch (err) {
console.log(err)
useState('user', () => null)
return navigateTo('/login')
}
})