Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access Vuex storage mutation inside Axios interceptor

EDIT: this question was very chaotic so I basically rewrite it with same code example and same scenerio.


When 401 error is send in response from server I'm trying to .commit from interceptor to my vuex storage:

import axios from 'axios';
import { store } from '../store/store';

export default function execute() {
    axios.interceptors.request.use(function(config) {
        const token = store.state.token;
        if(token) {
            config.headers.Authorization = `Bearer ${token}`;
            //console.log(config);
            return config;
        } else {
            return config;
        }
    }, function(err) {
        return Promise.reject(err);
    });
    axios.interceptors.response.use((response) => {
        return response;
    }, (err) => {
        console.log(err.response.status);
        if(err.response.status === 401) {
            this.$store.commit('logout');
        }
        console.log('err'); // this doesnt even console log
        return Promise.reject(err);
    });
}

But it's throwing error

TypeError: Cannot read property '$store' of undefined

at eval (httpInterceptor.js?bdcd:22)

I don't know why I think that I import it correctly.

I use arrow functions, I also tried without arrow functions but it cause same error as above.

I execute my interceptor in main.js by importing it and calling (import interceptor from './helpers/httpInterceptor.js'; interceptor();)

My vuex storage file is:

import Vue from 'vue';
import Vuex from 'vuex';
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex);

export const store = new Vuex.Store({
    state: {
        logged: false,
        token: ''
    },
    mutations: {
        login: (state, response) => {
            if(response) {
                state.logged = true;
                state.token = response;
                console.log('state updated');
                console.log('state.logged flag is: '+state.logged);
                console.log('state.token: '+state.token);
            }
        },
        logout: (state) => {
            state.logged = false;
            state.token = '';
        }
    },
    plugins: [
        createPersistedState()
    ]
});

and I'm assigning it to Vue instance

new Vue({
    el: '#app',
    router,
    store,
    template: '<app/>',
    components: { app }
});

Why I can't use my mutation inside this interceptor, what is causing issue and how to fix it?

like image 952
BT101 Avatar asked Apr 03 '18 14:04

BT101


1 Answers

Alright I fixed it. So what I did wrong is that I follow documentation examples where it was like this.$store.... and it's fine when you use it inside component methods. When I imported it like a variable

import { store } from '../store/store';

I should change

this.$store.commit('logout');

to

store.commit('logout');

and now it's working fine.

like image 152
BT101 Avatar answered Nov 15 '22 06:11

BT101