Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda