nuxt 3 onload wait for onAuthStateChange

Viewed 309

im trying to use nuxt 3, pinia and firebase on auth transaction in my application

everything is working fine about onAuthStateChange except when I refresh a page. in this case im getting a kind of delay until load the current user data from firebase and set it with pinia store.

At this time i have a store, where i run onAuthStateChange. if has user, I set the data on state, and return this store as a Promisse

currentUser() {
  const auth = getAuth()
  return new Promise((resolve, reject) => {
    onAuthStateChanged(auth, (user) => {
      if (user) {
        console.log("user", user)
        resolve(user)
        this.setUser(user)
      } else {
        console.log("no user")
        resolve(undefined)
      }
    })
  })
},

and, I have a plugin, called init, where i call this store.

export default defineNuxtPlugin((nuxtApp) => {
    const store = useAuthStore()
    store.currentUser()
})

my question is. how can i make my application wait for this transaction before loading all?

just one example of what this cause, is where i using a getter to show or not a login form. the form is appearing in begining, and then desappear if user is loggedin.

<form-signin v-if="!store.loggedin" />
<div v-else>already logged as {{ store.userData.email }}</div>
0 Answers
Related