In main.js
import axios from 'axios';
axios.defaults.headers.common = {
'Authorization': 'JWT ' + Vue.auth.getToken()
};
axios.defaults.baseURL = process.env.VUE_APP_BASE_URL; //TODO: ensure the trailing slash is appended
// Add modified axios instance to Vue prototype so that it is available globally via Vue instance
Vue.prototype.$http = axios;
Everything was working fine until this point. (I could successfully log in and store the token)
However, I have encountered an issue with another component that fetches a list of users from the server through an ajax call in the component's created() lifecycle hook.
The problem arises when I try to access this.$http
within the component - I receive a 401 error response from the server because the Authorization
header is not included in the request headers, despite having pre-configured axios.defaults.headers.common.
What's strange is that if I manually refresh the page, the token is correctly attached to the request header and the user list is successfully retrieved**.**
Could someone please provide an explanation for why this behavior is occurring?