#What is the best way to wait for state to be ready?

4 messages · Page 1 of 1 (latest)

radiant marsh
#

I have an auth.ts middlware which checks if user is logged in or not:

export default defineNuxtRouteMiddleware(async (to, from) => {
  if (!process.server) {
  const firebaseUser = useUserStore()
  if (
    !firebaseUser.value &&
    to.path !== '/auth/signin' &&
    to.path !== '/auth/signup'
  ) {
    return navigateTo('/auth/signin', { replace: true })
  }}
})

The issue is that middleware fires before Firebase initializing. So when middleware is fired, my firebaseUser won't be ready.
Any work arounds for it?

old pumice
#

@radiant marsh where firebase will initialize then? Where are you doing the function call to do so?

radiant marsh
#

@old pumice
Sorry for unclear description.

Firebase is initialized through plugins and provided to Nuxt with 'provide'.

  return {
    provide: {
      firebase: firebaseApp,
      firestore: firestore,
      auth: auth,
    },
  }

In app.vue I have authChecker, which calls Firebase’s onAuthStateChange, and check if user is present and saves the user to state.

onMounted(() => {
  checkAuthState()
})
old pumice