fix cookie token bug
This commit is contained in:
parent
4321f5845f
commit
822c59bdba
|
@ -11,6 +11,7 @@ import 'nprogress/nprogress.css'
|
||||||
import 'normalize.css/normalize.css'
|
import 'normalize.css/normalize.css'
|
||||||
import '@/assets/iconfont/iconfont'
|
import '@/assets/iconfont/iconfont'
|
||||||
import IconSvg from '@/components/Icon-svg/index.vue'
|
import IconSvg from '@/components/Icon-svg/index.vue'
|
||||||
|
import { getToken } from '@/utils/auth'
|
||||||
|
|
||||||
Vue.config.productionTip = false
|
Vue.config.productionTip = false
|
||||||
|
|
||||||
|
@ -20,7 +21,7 @@ Vue.component('icon-svg', IconSvg)
|
||||||
const whiteList = ['/login'];
|
const whiteList = ['/login'];
|
||||||
router.beforeEach((to, from, next) => {
|
router.beforeEach((to, from, next) => {
|
||||||
NProgress.start();
|
NProgress.start();
|
||||||
if (store.getters.token) {
|
if (getToken()) {
|
||||||
if (to.path === '/login') {
|
if (to.path === '/login') {
|
||||||
next({ path: '/' });
|
next({ path: '/' });
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import { login, logout, getInfo } from '@/api/login';
|
import { login, logout, getInfo } from '@/api/login';
|
||||||
import Cookies from 'js-cookie';
|
import { getToken, setToken, removeToken } from '@/utils/auth';
|
||||||
|
|
||||||
const user = {
|
const user = {
|
||||||
state: {
|
state: {
|
||||||
token: Cookies.get('Admin-Token'),
|
token: getToken(),
|
||||||
name: '',
|
name: '',
|
||||||
avatar: '',
|
avatar: '',
|
||||||
roles: []
|
roles: []
|
||||||
|
@ -31,7 +31,7 @@ const user = {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
login(email, userInfo.password).then(response => {
|
login(email, userInfo.password).then(response => {
|
||||||
const data = response.data;
|
const data = response.data;
|
||||||
Cookies.set('Admin-Token', data.token);
|
setToken(data.token);
|
||||||
commit('SET_TOKEN', data.token);
|
commit('SET_TOKEN', data.token);
|
||||||
resolve();
|
resolve();
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
|
@ -62,7 +62,7 @@ const user = {
|
||||||
logout(state.token).then(() => {
|
logout(state.token).then(() => {
|
||||||
commit('SET_TOKEN', '');
|
commit('SET_TOKEN', '');
|
||||||
commit('SET_ROLES', []);
|
commit('SET_ROLES', []);
|
||||||
Cookies.remove('Admin-Token');
|
removeToken();
|
||||||
resolve();
|
resolve();
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
reject(error);
|
reject(error);
|
||||||
|
@ -74,7 +74,7 @@ const user = {
|
||||||
FedLogOut({ commit }) {
|
FedLogOut({ commit }) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
commit('SET_TOKEN', '');
|
commit('SET_TOKEN', '');
|
||||||
Cookies.remove('Admin-Token');
|
removeToken();
|
||||||
resolve();
|
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,6 +1,7 @@
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { Message } from 'element-ui';
|
import { Message } from 'element-ui';
|
||||||
import store from '../store';
|
import store from '../store';
|
||||||
|
import { getToken } from '@/utils/auth';
|
||||||
|
|
||||||
|
|
||||||
// 创建axios实例
|
// 创建axios实例
|
||||||
|
@ -12,7 +13,7 @@ const service = axios.create({
|
||||||
// request拦截器
|
// request拦截器
|
||||||
service.interceptors.request.use(config => {
|
service.interceptors.request.use(config => {
|
||||||
if (store.getters.token) {
|
if (store.getters.token) {
|
||||||
config.headers['X-Token'] = store.getters.token; // 让每个请求携带自定义token 请根据实际情况自行修改
|
config.headers['X-Token'] = getToken(); // 让每个请求携带自定义token 请根据实际情况自行修改
|
||||||
}
|
}
|
||||||
return config;
|
return config;
|
||||||
}, error => {
|
}, error => {
|
||||||
|
|
Loading…
Reference in New Issue