Add header token to axios requests after login action in vuex ... | vue js axios token
I’mreallystuckonthis.InthevuexstoreIamimportingafiletoconfigureaxios:importHTTPfrom../http-commonThecontentsofthefilelookslikethisimportaxiosfromaxiosconstaxiosApi=axios.create({baseURL:(process.env.VUE_APP_BASE_URL!==undefined)?process.env.VUE_APP_BASE_URL://trackerapp.local/})exportconstsetAuthHeader=(token)=>{axiosApi.defaults.headers.common[Authorization]=`Bearer${token}`}setAuthHeader(localStorage.getItem(authtoken))exportdefaultaxiosApi;Iwanteveryaxiosrequesttocheckforalocalstoraget...
I’m really stuck on this.
In the vuex store I am importing a file to configure axios:
import HTTP from ../http-common
The contents of the file looks like this
import axios from axios const axiosApi = axios.create({ baseURL: (process.env.VUE_APP_BASE_URL !== undefined) ? process.env.VUE_APP_BASE_URL : //trackerapp.local/ }) export const setAuthHeader = (token) => { axiosApi.defaults.headers.common[Authorization] = `Bearer ${token}` } setAuthHeader(localStorage.getItem(authtoken)) export default axiosApi;I want every axios request to check for a local storage token every time. But it does not.
The value of the token is null, until I refresh the page. Then the token gets added to the headers.