Polarcraft/webv2/middleware/auth.ts
Xeovalyte d74a51db7f
All checks were successful
Build and Deploy / Deploy Web (push) Successful in 1m2s
Build and Deploy / Deploy Discord Bot (push) Successful in 44s
feat: Added verify account route, closes #28
2023-06-05 16:05:03 +02:00

26 lines
518 B
TypeScript

export default defineNuxtRouteMiddleware(async (to) => {
if (process.server) {
return
}
try {
const user = await $fetch('/api/users/@me')
if (!user._id) {
throw createError({ statusCode: 500, statusMessage: 'No user was found' })
}
useState('user', () => user)
if (to.meta.moderator && !user.role.moderator) {
return navigateTo('/')
}
} catch (e) {
console.error('Failed to get user', e)
useState('user', () => null)
return navigateTo('/login')
}
})