I have a Vue SPA and I'm using Axios for handling requests, vue-axios to apply it and vuex for storing tokens. But when I send a request the header Authorization becomes Bearer undefined or Bearer null.
The flow is that by logging in I retrieve a token, save it to the session storage, dispatch a login action and my vuex store will set a token in the state from the session storage. After my login and router redirect to "/" I want to send a GET request to /foo along with my token. But here my token is undefined. Based on console logs I know that the session storage do have the token before the request is sent.
I am following the structure from this tutorial by Paweljw:
Axios/axios.js
import axios from 'axios'
export default axios.create({
baseURL: 'http://example.com/api/,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + sessionStorage.token
}
})
Axios/index.js
import Vue from 'vue';
import VueAxios from 'vue-axios';
import axios from './axios'
Vue.use(VueAxios, axios);
export default Vue;
Components/login.vue
onLogin(res) {
if (!res.data.token) {
return this.onLoginFailed();
}
sessionStorage.token = res.data.token;
this.$store.dispatch("login");
this.$router.replace(this.$route.query.redirect || "/");
Components/foo.vue
...
this.$http
.get("foo")
.then(res => this.doSomething(res))
.catch(() => this.doSomethingElse());
As previously mentioned this sends a request with Bearer undefined. I'm aware that when axios.create(...) is being exported the sessionStorage.token is null - it hasn't been set when the application starts. And that's probably why, but I don't understand how I'm supposed to set it then and how other people using the same tutorial doesn't have this issue.
Any advice is greatly appreciated.