我正在使用vuejs和vuex开发一个测试应用程序 . 我可以注册/注册用户,我也让用户登录并在localstorage vuex中设置令牌 . 但是,在刷新后,您在何处以及如何检查用户是否已登录?

我目前正在使用VUEX处理与axios的异步调用 . 我的“store”文件夹中的结构如下 - Index.js - actions.js - mutation.js - getters.js

这是我的行动的一部分.js

import config from './../config'
import axios from 'axios'
import Ls from '../mixins/localStorage'

axios.defaults.headers.common = {
    'Accept': 'application/json'
};

export default {
    login ({commit,dispatch}, user) {
        commit('setLoading', {
            modalName: 'loginModal',
            state: true
        });
        const endpoint = config.apiUrl + 'clients/web/login';
        axios.post(endpoint, user)
            .then(response => {
                Ls.set('token',response.data.request_key);
                dispatch('getUserData');
                return true;
            })
            .catch(e => {
                commit('setLoading', {
                    modalName: 'loginModal',
                    state: false
                });
                console.log(e);
                return false
            });
    },
    getUserData({commit}){
        const endpoint = config.apiUrl + 'user/profile';
        if(Ls.get('token')) {
            axios.defaults.headers.common['Authorization'] = 'Bearer ' + Ls.get('token').replace(/(^\")|(\"$)/g, '');
        }
        axios.get(endpoint)
            .then(response => {
                commit('setUser',response.data);
                commit('hideModal', 'loginModal');
            })
            .catch(e => {
                console.log(e);
                return false
            });
    }
}

所以问题是

  • 这是记录用户的正确方法吗?

  • 您如何以及在何处检查用户是否已登录?如果是,您接下来要做什么?

提前致谢!