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')
  }
})
