Hello,
To achieve this within a project I am developing I've extended the Axios default object and used interceptors to add the authorization token to every request axios will make.
import store from "@/store";
import Axios from "axios";
const axios = Axios.create({
baseURL: process.env.VUE_APP_API_URL,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
responseType: "json",
},
});
axios.interceptors.request.use((config) => {
const tokens = store.getters["auth/getTokens"];
if (!tokens) {
return config;
}
let token = tokens.access;
if (config.url === api.refresh) {
token = tokens.refresh;
}
config.headers.Authorization = `Bearer ${token}`;
return config;
});
export default axios;
Then it's a matter of only using this axios where needed like in store.