I would like to trigger a mutation from within an interceptor. In this case, trigger the logout mutation whenever Axios encounters an http 403.
I imported Vuex mutations and mapped as I've done elsewhere but I have not been able to access it within the Axios interceptor error function. I've added my interceptor config in App.vue's created() method.
I've tried the syntax from these 2 questions without success, most likely due to my project using modules, and the Axios config being located in a created() method.
- https://www.reddit.com/r/vuejs/comments/eq5eej/question_how_to_access_vuex_store_modules_mode_in/ <- Axios config in a seperate file, is this the best practice?
- Can't access Vuex storage mutation inside Axios interceptor <- Not using modules
App.vue
<script>
import { mapMutations } from "vuex";
import axios from 'axios';
export default {
methods: {
...mapMutations(["logout"])
},
created(){
axios.interceptors.response.use(
function (response) {
return response;
},
function (error) {
if (error.response.status === 403) {
this.logout()
.then(() => {
this.$router.push("/");
})
}
});
}
}
</script>
