Nuxt 3 - onServerPrefetch Lifecycle injection APIs

Viewed 230

[Vue warn]: onServerPrefetch is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup(). If you are using async setup(), make sure to register lifecycle hooks before the first await statement.

Composable

const isLoggerdIn = () => {
    return expire.value //return true or false
  }

Middleware

export default defineNuxtRouteMiddleware( (to) => {
  const { isLoggerdIn } = useAuth() // composable

  if (isLoggerdIn() && to.name == 'login'){
    return navigateTo("/home")
  }

  if (!isLoggerdIn() && to.name !== 'login'){
    return navigateTo("/login")
  }

})

On pages home and login

definePageMeta({
  middleware: 'auth'
})
0 Answers
Related