Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!