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

106
Views
Accessing state in actions vuex

I have a project where whenever the user logs in, it saves the authKey to store. I want to pass the authKey to the headers so it can get the API result. But, when I console it, the authKey is returning to null. I think this is my syntax of calling the authKey in state is wrong, but I can't seem to find the right way.

export default new Vuex.Store({ 
    state: {
        isLoggedIn: false,
        authKey: '',
        username: '',
        products: []
    },
    mutations: {
        LOGIN(state, payload) {
            state.isLoggedIn = true
            state.authKey = payload.authKey
            state.username = payload.username
        },
        SET_PRODUCTS(state, payload) {
            state.products = payload
        }
    },
    actions: {
        async getAll({commit, state}) {
            const config = {
                headers: {
                    Authorization : 'Bearer ' + state.authKey
                }
            }
            
            await axios.post('https://my-api-here.app', config)
                .then(async (res) => {
                    commit("SET_PRODUCTS", res.data.products)
                })
                .catch((err) => {
                    console.log(err)
                })
        }
    }
})
about 4 years ago · Juan Pablo Isaza
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!