fix: 修复不断登录的问题

This commit is contained in:
ibuler 2020-06-01 15:22:10 +08:00
parent 191ef09d61
commit 22ef377097
5 changed files with 22 additions and 36 deletions

View File

@ -65,7 +65,7 @@ export default {
window.location.href = `/` window.location.href = `/`
break break
case 'logout': case 'logout':
window.location.href = `/core/auth/logout/?next=${this.$route.fullPath}` window.location.href = `${process.env.VUE_APP_LOGOUT_PATH}?next=${this.$route.fullPath}`
break break
case 'apiKey': case 'apiKey':
this.$refs.api.showApi() this.$refs.api.showApi()

View File

@ -13,11 +13,11 @@ router.beforeEach(async(to, from, next) => {
NProgress.start() NProgress.start()
try { try {
await startup({ to, from, next }) await startup({ to, from, next })
await getUserRoleAndSetRoutes({ to, from, next }) // await getUserRoleAndSetRoutes({ to, from, next })
} catch (e) { } catch (e) {
console.log('Start service error: ', e) console.log('Start service error: ', e)
} }
NProgress.done() next()
}) })
router.afterEach(() => { router.afterEach(() => {

View File

@ -51,10 +51,10 @@ service.interceptors.request.use(
) )
function ifUnauthorized({ response, error }) { function ifUnauthorized({ response, error }) {
if (response.status === 401) { if (response.status === 401 && response.config.url.indexOf('/users/profile') === -1) {
// 未授权重定向到登录页面 // 未授权重定向到登录页面
const title = i18n.t('common.Info').String() const title = i18n.t('common.Info')
const msg = i18n.t('auth.LoginRequiredMsg').String() const msg = i18n.t('auth.LoginRequiredMsg')
MessageBox.confirm(msg, title, { MessageBox.confirm(msg, title, {
confirmButtonText: i18n.t('auth.ReLogin'), confirmButtonText: i18n.t('auth.ReLogin'),
cancelButtonText: i18n.t('common.Cancel'), cancelButtonText: i18n.t('common.Cancel'),

View File

@ -5,32 +5,29 @@ import { Message } from 'element-ui'
import 'nprogress/nprogress.css' // progress bar style import 'nprogress/nprogress.css' // progress bar style
import { getPermission, getToken, setPermission } from '@/utils/auth' import { getPermission, getToken, setPermission } from '@/utils/auth'
const whiteList = ['/login'] // no redirect whitelist const whiteList = ['/login', process.env.VUE_APP_LOGIN_PATH] // no redirect whitelist
let initial = false let initial = false
function setHeadTitle({ to, from, next }) { function setHeadTitle({ to, from, next }) {
document.title = getPageTitle(to.meta.title) document.title = getPageTitle(to.meta.title)
} }
function checkLogin({ to, from, next }) { async function checkLogin({ to, from, next }) {
if (whiteList.indexOf(to.path) !== -1) {
next()
}
// determine whether the user has logged in // determine whether the user has logged in
const hasToken = getToken() const hasToken = getToken()
if (!hasToken) { if (!hasToken) {
/* has no token*/ window.location = process.env.VUE_APP_LOGIN_PATH
if (whiteList.indexOf(to.path) !== -1) {
next()
} else {
// other pages that do not have permission to access are redirected to the login page.
next(process.env.LOGIN_PATH)
return return
} }
}
if (to.path === '/login') { try {
// if is logged in, redirect to the home page return await store.dispatch('users/getProfile')
next({ path: '/' }) } catch (e) {
return // return false
window.location = process.env.VUE_APP_LOGIN_PATH
} }
} }
@ -44,12 +41,7 @@ async function getPublicSetting({ to, from, next }) {
export async function getUserRoleAndSetRoutes({ to, from, next }) { export async function getUserRoleAndSetRoutes({ to, from, next }) {
// determine whether the user has obtained his permission roles through getProfile // determine whether the user has obtained his permission roles through getProfile
const currentUser = store.getters.currentUser
const hasRoles = currentUser && currentUser.current_org_roles && currentUser.current_org_roles.length > 0
if (hasRoles) {
next()
return
}
try { try {
// try get user profile // try get user profile
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor'] // note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
@ -76,8 +68,6 @@ export async function getUserRoleAndSetRoutes({ to, from, next }) {
// await store.dispatch('user/resetToken') // await store.dispatch('user/resetToken')
Message.error(error || 'Has Error') Message.error(error || 'Has Error')
console.log('Error occur: ', error) console.log('Error occur: ', error)
next(`/core/auth/login/`)
// next()
} }
} }
@ -89,14 +79,10 @@ export async function startup({ to, from, next }) {
initial = true initial = true
// set page title // set page title
setHeadTitle({ to, from, next }) await setHeadTitle({ to, from, next })
// console.log('Set head title') await checkLogin({ to, from, next })
checkLogin({ to, from, next })
// console.log('Check login')
await getPublicSetting({ to, from, next }) await getPublicSetting({ to, from, next })
// console.log('Get public setting')
await getUserRoleAndSetRoutes({ to, from, next }) await getUserRoleAndSetRoutes({ to, from, next })
// console.log('Get profile')
return true return true
} }

View File

@ -47,7 +47,7 @@ module.exports = {
['^' + process.env.VUE_APP_BASE_API]: '' ['^' + process.env.VUE_APP_BASE_API]: ''
} }
}, },
'^/core/': { '^/(core|static|media)/': {
target: process.env.VUE_APP_CORE_HOST, target: process.env.VUE_APP_CORE_HOST,
changeOrigin: true changeOrigin: true
} }