2023-06-04 22:47:48 +02:00
|
|
|
export default defineNuxtRouteMiddleware(async (to) => {
|
|
|
|
if (process.server) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-06-06 13:50:10 +02:00
|
|
|
const user = await $fetch('/api/users/@me') as IUser
|
2023-06-04 22:47:48 +02:00
|
|
|
|
2023-06-05 14:55:35 +02:00
|
|
|
if (!user._id) {
|
2023-06-04 22:47:48 +02:00
|
|
|
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')
|
|
|
|
}
|
|
|
|
})
|