From 822c59bdba0d5d27c315a6bb80ec1b847f0d3b8e Mon Sep 17 00:00:00 2001 From: Pan Date: Thu, 20 Jul 2017 14:27:47 +0800 Subject: [PATCH] fix cookie token bug --- src/main.js | 3 ++- src/store/modules/user.js | 10 +++++----- src/utils/auth.js | 15 +++++++++++++++ src/utils/fetch.js | 3 ++- 4 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 src/utils/auth.js diff --git a/src/main.js b/src/main.js index 769fd76..40793c8 100644 --- a/src/main.js +++ b/src/main.js @@ -11,6 +11,7 @@ import 'nprogress/nprogress.css' import 'normalize.css/normalize.css' import '@/assets/iconfont/iconfont' import IconSvg from '@/components/Icon-svg/index.vue' +import { getToken } from '@/utils/auth' Vue.config.productionTip = false @@ -20,7 +21,7 @@ Vue.component('icon-svg', IconSvg) const whiteList = ['/login']; router.beforeEach((to, from, next) => { NProgress.start(); - if (store.getters.token) { + if (getToken()) { if (to.path === '/login') { next({ path: '/' }); } else { diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 3459191..9fc332c 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,9 +1,9 @@ import { login, logout, getInfo } from '@/api/login'; -import Cookies from 'js-cookie'; +import { getToken, setToken, removeToken } from '@/utils/auth'; const user = { state: { - token: Cookies.get('Admin-Token'), + token: getToken(), name: '', avatar: '', roles: [] @@ -31,7 +31,7 @@ const user = { return new Promise((resolve, reject) => { login(email, userInfo.password).then(response => { const data = response.data; - Cookies.set('Admin-Token', data.token); + setToken(data.token); commit('SET_TOKEN', data.token); resolve(); }).catch(error => { @@ -62,7 +62,7 @@ const user = { logout(state.token).then(() => { commit('SET_TOKEN', ''); commit('SET_ROLES', []); - Cookies.remove('Admin-Token'); + removeToken(); resolve(); }).catch(error => { reject(error); @@ -74,7 +74,7 @@ const user = { FedLogOut({ commit }) { return new Promise(resolve => { commit('SET_TOKEN', ''); - Cookies.remove('Admin-Token'); + removeToken(); resolve(); }); } diff --git a/src/utils/auth.js b/src/utils/auth.js new file mode 100644 index 0000000..08a43d6 --- /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 05c5a0e..52817b9 100644 --- a/src/utils/fetch.js +++ b/src/utils/fetch.js @@ -1,6 +1,7 @@ import axios from 'axios'; import { Message } from 'element-ui'; import store from '../store'; +import { getToken } from '@/utils/auth'; // 创建axios实例 @@ -12,7 +13,7 @@ const service = axios.create({ // request拦截器 service.interceptors.request.use(config => { if (store.getters.token) { - config.headers['X-Token'] = store.getters.token; // 让每个请求携带自定义token 请根据实际情况自行修改 + config.headers['X-Token'] = getToken(); // 让每个请求携带自定义token 请根据实际情况自行修改 } return config; }, error => {