fix cookie token bug

pull/136/head
Pan 7 years ago
parent c8856741c6
commit a0a011e985

@ -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 {

@ -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();
})
}

@ -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)
}

@ -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 => {

Loading…
Cancel
Save