Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '$store') in Nuxt js and firebase authentication

I am implementing firebase authentication to Nuxt js application and I am so close. The problem is I want to commit a vuext mutation inside firebase's default function onAuthStateChanged(). But when ever I load the page it shows the following error: "Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '$store')"

Can you guys please help me out with this problem.

Thanks.

import firebase from '@/plugins/firebase'

import {
  getAuth,
  signInWithEmailAndPassword,
  onAuthStateChanged
} from "firebase/auth"

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

export const mutations = {
  updateUser(state, payload) {
    state.user = payload
    console.log('user is updated', state.user)
  },

  setAuthIsReady(state, payload) {
    state.authIsReady = payload
    console.log(state.authIsReady)
  }
}

export const actions = {
  async signIn(context, {
    email,
    password
  }) {
    console.log('sign in action')

    const res = await signInWithEmailAndPassword(getAuth(), email, password)
    if (res) {
      context.commit('updateUser', res.user)
    } else {
      throw new Error('could not complete sign in')
    }
  }
}

// this function is causing the problem

const unsub = onAuthStateChanged(getAuth(), (user) => {
  this.$store.commit('updateUser', user)
  unsub()
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The firebase.js file that I'm importing "auth" from below, is just all the regular setting up Firebase in Nuxt stuff... and the important lines are: const auth = getAuth() export { auth }

  1. Try the code below ... I have mine in a file named "fireauth.js" in the plugins folder (don't forget to import the "fireauth.js" file in your nuxt.config.js)

 import {
    auth
} from "~/plugins/firebase.js";

export default (context) => {
    const {
        store
    } = context

    return new Promise((resolve, reject) => {
        auth.onAuthStateChanged((user) => {
            if (user) {
                return resolve(store.dispatch('onAuthStateChangedAction', user))
            }
            return resolve()
        })
    })
}

  1. In your store/index.js file add the following async function in your actions setting:

     async onAuthStateChangedAction(vuexContext, authUser) {
    
    
     if (!authUser) { //in my case I'm just forcing user back to sign in page, only authorized users allowed//redirect from here this.$router.push({
             path: '/signin',
         })
     }else {
         //call your commits or do whatever you want to do
         vuexContext.commit("setUser", authUser.email);
     }
    

    },


The first part of the code ensures that when the auth state changes in Firestore, this change is communicated to the action that you just created in the store. The second part of the code, the async function in the store accomplishes whatever you want it to do within the store.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda