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)
})
}
}
})