From a0a011e985747d58b469be224e3d2dadf5885605 Mon Sep 17 00:00:00 2001 From: Pan Date: Thu, 20 Jul 2017 13:26:09 +0800 Subject: [PATCH] fix cookie token bug --- src/main.js | 3 ++- src/store/modules/user.js | 14 +++++++------- src/utils/auth.js | 15 +++++++++++++++ src/utils/fetch.js | 4 ++-- 4 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 src/utils/auth.js diff --git a/src/main.js b/src/main.js index 68bb5661..09e25466 100644 --- a/src/main.js +++ b/src/main.js @@ -19,6 +19,7 @@ import IconSvg from 'components/Icon-svg';// svg 组件 import vueWaves from './directive/waves';// 水波纹指令 import errLog from 'store/errLog';// error log组件 import './mock/index.js'; // 该项目所有请求使用mockjs模拟 +import { getToken } from 'utils/auth'; // register globally Vue.component('multiselect', Multiselect); @@ -43,7 +44,7 @@ function hasPermission(roles, permissionRoles) { const whiteList = ['/login', '/authredirect', '/reset', '/sendpwd'];// 不重定向白名单 router.beforeEach((to, from, next) => { NProgress.start(); // 开启Progress - if (store.getters.token) { // 判断是否有token + if (getToken()) { // 判断是否有token if (to.path === '/login') { next({ path: '/' }); } else { diff --git a/src/store/modules/user.js b/src/store/modules/user.js index b6f2b8bd..d65175eb 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,12 +1,12 @@ import { loginByEmail, logout, getInfo } from 'api/login'; -import Cookies from 'js-cookie'; +import { getToken, setToken, removeToken } from 'utils/auth'; const user = { state: { user: '', status: '', code: '', - token: Cookies.get('Admin-Token'), + token: getToken(), name: '', avatar: '', introduction: '', @@ -56,7 +56,7 @@ const user = { return new Promise((resolve, reject) => { loginByEmail(email, userInfo.password).then(response => { const data = response.data; - Cookies.set('Admin-Token', response.data.token); + setToken(response.data.token); commit('SET_TOKEN', data.token); resolve(); }).catch(error => { @@ -87,7 +87,7 @@ const user = { commit('SET_CODE', code); loginByThirdparty(state.status, state.email, state.code).then(response => { commit('SET_TOKEN', response.data.token); - Cookies.set('Admin-Token', response.data.token); + setToken(response.data.token); resolve(); }).catch(error => { reject(error); @@ -101,7 +101,7 @@ const user = { logout(state.token).then(() => { commit('SET_TOKEN', ''); commit('SET_ROLES', []); - Cookies.remove('Admin-Token'); + removeToken(); resolve(); }).catch(error => { reject(error); @@ -113,7 +113,7 @@ const user = { FedLogOut({ commit }) { return new Promise(resolve => { commit('SET_TOKEN', ''); - Cookies.remove('Admin-Token'); + removeToken(); resolve(); }); }, @@ -123,7 +123,7 @@ const user = { return new Promise(resolve => { commit('SET_ROLES', [role]); commit('SET_TOKEN', role); - Cookies.set('Admin-Token', role); + setToken(role); resolve(); }) } diff --git a/src/utils/auth.js b/src/utils/auth.js new file mode 100644 index 00000000..08a43d6e --- /dev/null +++ b/src/utils/auth.js @@ -0,0 +1,15 @@ +import Cookies from 'js-cookie' + +const TokenKey = 'Admin-Token' + +export function getToken() { + return Cookies.get(TokenKey) +} + +export function setToken(token) { + return Cookies.set(TokenKey, token) +} + +export function removeToken() { + return Cookies.remove(TokenKey) +} diff --git a/src/utils/fetch.js b/src/utils/fetch.js index 43caa095..cf15ca75 100644 --- a/src/utils/fetch.js +++ b/src/utils/fetch.js @@ -1,7 +1,7 @@ import axios from 'axios'; import { Message } from 'element-ui'; import store from '../store'; -// import router from '../router'; +import { getToken } from 'utils/auth'; // 创建axios实例 const service = axios.create({ @@ -13,7 +13,7 @@ const service = axios.create({ service.interceptors.request.use(config => { // Do something before request is sent if (store.getters.token) { - config.headers['X-Token'] = store.getters.token; // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改 + config.headers['X-Token'] = getToken(); // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改 } return config; }, error => {