I am working with an axios apiClient
and trying to retrieve the email stored in localStorage
to use in the header of my component. I attempted to access the email by using
response.headers['email']
and storing it in a variable called email, but unfortunately, I am receiving undefined. While I can successfully fetch the email from localStorage
, I am encountering difficulties passing it to the component. Any assistance on this matter would be greatly appreciated.
Axios
const apiClient = axios.create({
baseURL: `${API}`,
withCredentials: false,
headers: {
Accept: "application/json",
"Content-Type": "application/json"
}
});
apiClient.interceptors.request.use(function (config) {
let token = JSON.parse(localStorage.getItem('token'));
let email = JSON.parse(localStorage.getItem('email'));
if (token) {
config.headers.Authorization = `Bearer ${token}`;
config.headers.email = `${email}`;
}
return config;
}, function (err) {
return Promise.reject(err);
});
The following is the method within my component where I require access to the email data
methods: {
getOrders(){
service.getAllOrders()
.then(response => {
this.email = response.headers['email'];
console.log("email:", this.email)
})
}
}
getAllOrders()
makes an axios get
request.