I've been working on integrating the Amazon Cognito Vuex Module
into my Vue.js app to automatically pass credentials with all axios
requests. Here's the code I'm using:
// Add authentication token to each request
axios.interceptors.request.use(async config => {
const response = await store.dispatch('getUserSession');
if (response && response.accessToken && response.accessToken.jwtToken) {
config.headers.AccessToken = response.accessToken.jwtToken;
}
return config;
});
It seems like this should be a standard piece of code that needs to run for all components, but it's unclear where exactly to add it. Should it go in App.vue
or index.js
? In my App.vue
, I currently have:
import Vue from 'vue';
import VueRouter from 'vue-router';
import Vuetify from 'vuetify';
Vue.use(Vuetify);
Vue.use(VueRouter);
export default new Vue({}).$mount('#app');
And in my index.js
:
export default new Vuex.Store({
state: {
...