forked from AkkomaGang/admin-fe
fix cookie token bug
This commit is contained in:
parent
c8856741c6
commit
a0a011e985
4 changed files with 26 additions and 10 deletions
|
@ -19,6 +19,7 @@ import IconSvg from 'components/Icon-svg';// svg 组件
|
||||||
import vueWaves from './directive/waves';// 水波纹指令
|
import vueWaves from './directive/waves';// 水波纹指令
|
||||||
import errLog from 'store/errLog';// error log组件
|
import errLog from 'store/errLog';// error log组件
|
||||||
import './mock/index.js'; // 该项目所有请求使用mockjs模拟
|
import './mock/index.js'; // 该项目所有请求使用mockjs模拟
|
||||||
|
import { getToken } from 'utils/auth';
|
||||||
|
|
||||||
// register globally
|
// register globally
|
||||||
Vue.component('multiselect', Multiselect);
|
Vue.component('multiselect', Multiselect);
|
||||||
|
@ -43,7 +44,7 @@ function hasPermission(roles, permissionRoles) {
|
||||||
const whiteList = ['/login', '/authredirect', '/reset', '/sendpwd'];// 不重定向白名单
|
const whiteList = ['/login', '/authredirect', '/reset', '/sendpwd'];// 不重定向白名单
|
||||||
router.beforeEach((to, from, next) => {
|
router.beforeEach((to, from, next) => {
|
||||||
NProgress.start(); // 开启Progress
|
NProgress.start(); // 开启Progress
|
||||||
if (store.getters.token) { // 判断是否有token
|
if (getToken()) { // 判断是否有token
|
||||||
if (to.path === '/login') {
|
if (to.path === '/login') {
|
||||||
next({ path: '/' });
|
next({ path: '/' });
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import { loginByEmail, logout, getInfo } from 'api/login';
|
import { loginByEmail, logout, getInfo } from 'api/login';
|
||||||
import Cookies from 'js-cookie';
|
import { getToken, setToken, removeToken } from 'utils/auth';
|
||||||
|
|
||||||
const user = {
|
const user = {
|
||||||
state: {
|
state: {
|
||||||
user: '',
|
user: '',
|
||||||
status: '',
|
status: '',
|
||||||
code: '',
|
code: '',
|
||||||
token: Cookies.get('Admin-Token'),
|
token: getToken(),
|
||||||
name: '',
|
name: '',
|
||||||
avatar: '',
|
avatar: '',
|
||||||
introduction: '',
|
introduction: '',
|
||||||
|
@ -56,7 +56,7 @@ const user = {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
loginByEmail(email, userInfo.password).then(response => {
|
loginByEmail(email, userInfo.password).then(response => {
|
||||||
const data = response.data;
|
const data = response.data;
|
||||||
Cookies.set('Admin-Token', response.data.token);
|
setToken(response.data.token);
|
||||||
commit('SET_TOKEN', data.token);
|
commit('SET_TOKEN', data.token);
|
||||||
resolve();
|
resolve();
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
|
@ -87,7 +87,7 @@ const user = {
|
||||||
commit('SET_CODE', code);
|
commit('SET_CODE', code);
|
||||||
loginByThirdparty(state.status, state.email, state.code).then(response => {
|
loginByThirdparty(state.status, state.email, state.code).then(response => {
|
||||||
commit('SET_TOKEN', response.data.token);
|
commit('SET_TOKEN', response.data.token);
|
||||||
Cookies.set('Admin-Token', response.data.token);
|
setToken(response.data.token);
|
||||||
resolve();
|
resolve();
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
reject(error);
|
reject(error);
|
||||||
|
@ -101,7 +101,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);
|
||||||
|
@ -113,7 +113,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();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
@ -123,7 +123,7 @@ const user = {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
commit('SET_ROLES', [role]);
|
commit('SET_ROLES', [role]);
|
||||||
commit('SET_TOKEN', role);
|
commit('SET_TOKEN', role);
|
||||||
Cookies.set('Admin-Token', role);
|
setToken(role);
|
||||||
resolve();
|
resolve();
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
15
src/utils/auth.js
Normal file
15
src/utils/auth.js
Normal file
|
@ -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 axios from 'axios';
|
||||||
import { Message } from 'element-ui';
|
import { Message } from 'element-ui';
|
||||||
import store from '../store';
|
import store from '../store';
|
||||||
// import router from '../router';
|
import { getToken } from 'utils/auth';
|
||||||
|
|
||||||
// 创建axios实例
|
// 创建axios实例
|
||||||
const service = axios.create({
|
const service = axios.create({
|
||||||
|
@ -13,7 +13,7 @@ const service = axios.create({
|
||||||
service.interceptors.request.use(config => {
|
service.interceptors.request.use(config => {
|
||||||
// Do something before request is sent
|
// Do something before request is sent
|
||||||
if (store.getters.token) {
|
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;
|
return config;
|
||||||
}, error => {
|
}, error => {
|
||||||
|
|
Loading…
Reference in a new issue