nuxt-community / firebase-module

🔥 Easily integrate Firebase into your Nuxt project. 🔥
https://firebase.nuxtjs.org
MIT License
642 stars 98 forks source link

Router redirects on signOut #351

Closed cliffordh closed 4 years ago

cliffordh commented 4 years ago

When I call:

        await this.$fireAuth.signOut().then((userCredential) => {})

I am signed out of Firebase, but my site redirects to the home page when this function executes. I can see that onAuthStateChangedAction in the store on signout. But I can't tell where this redirection behavior is coming from. Any ideas?

This is my store/index.js:

export const state = () => ({
  admin: false,
  user: null,
})

export const getters = {
  isAdmin: (state) => {
    return state.admin
  },
  user: (state) => {
    return state.user
  },
}

export const mutations = {
  setAdmin(state, isAdmin) {
    state.admin = isAdmin
  },
  setUser(state, authUser) {
    if (authUser == null) {
      state.user = null
      state.admin = false
    } else {
      const { uid, email, emailVerified, displayName, photoURL } = authUser
      state.user = {
        uid,
        displayName,
        email,
        emailVerified,
        photoURL: photoURL || null, // results in photoURL being null for server auth
        // use custom claims to control access (see https://firebase.google.com/docs/auth/admin/custom-claims)
        //isAdmin: claims.custom_claim,
      }
    }
  },
}

export const actions = {
  onAuthStateChangedAction({ commit }, { authUser, claims }) {
    console.log('nuxt/firebase action invoke')
    if (!authUser) {
      console.log('setAdmin: false')
      commit('setAdmin', false)
      commit('setUser', null)
      // claims = null
      // Perform logout operations
    } else {
      // Do something with the authUser and the claims object...

      //      authUser.getIdTokenResult().then((idTokenResult) => {
      // Get the decoded claims.
      //        const { claims } = idTokenResult
      // Get the "admin" attribute.
      const { admin } = claims
      commit('setAdmin', admin)
      console.log('setAdmin: ' + admin)
      commit('setUser', authUser)
      console.log('setUser: ' + authUser)
      //      })
    }
  },
  refreshToken({ commit }, user) {
    if (!user) {
      console.log('setAdmin: false')
      commit('setAdmin', false)
    } else {
      user.getIdTokenResult().then((idTokenResult) => {
        // Get the decoded claims.
        const { claims } = idTokenResult
        // Get the "admin" attribute.
        const { admin } = claims
        commit('setAdmin', admin)
        console.log('setAdmin: ' + admin)
      })
    }
  },
}
lupas commented 4 years ago

Hm.. calling signOut indeed triggers onAuthStateChangedAction as it seems to work for you, but it does nothing like redirect you to another route...

In your code I can't see anything that would trigger a route change - do you maybe have a listener on your user store object defined somewhere in your views or do you e.g. use any middleware?

cliffordh commented 4 years ago

@lupas I had a listener in my layout that was causing the redirect. Thanks for the assist. Closing now.