1
0
mirror of https://github.com/jumpserver/lina.git synced 2025-05-10 17:16:25 +00:00

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 = `/`
break
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
case 'apiKey':
this.$refs.api.showApi()

View File

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

View File

@ -51,10 +51,10 @@ service.interceptors.request.use(
)
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 msg = i18n.t('auth.LoginRequiredMsg').String()
const title = i18n.t('common.Info')
const msg = i18n.t('auth.LoginRequiredMsg')
MessageBox.confirm(msg, title, {
confirmButtonText: i18n.t('auth.ReLogin'),
cancelButtonText: i18n.t('common.Cancel'),

View File

@ -5,32 +5,29 @@ import { Message } from 'element-ui'
import 'nprogress/nprogress.css' // progress bar style
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
function setHeadTitle({ to, from, next }) {
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
const hasToken = getToken()
if (!hasToken) {
/* has no token*/
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
}
window.location = process.env.VUE_APP_LOGIN_PATH
return
}
if (to.path === '/login') {
// if is logged in, redirect to the home page
next({ path: '/' })
return
try {
return await store.dispatch('users/getProfile')
} catch (e) {
// 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 }) {
// 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 get user profile
// 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')
Message.error(error || 'Has Error')
console.log('Error occur: ', error)
next(`/core/auth/login/`)
// next()
}
}
@ -89,14 +79,10 @@ export async function startup({ to, from, next }) {
initial = true
// set page title
setHeadTitle({ to, from, next })
// console.log('Set head title')
checkLogin({ to, from, next })
// console.log('Check login')
await setHeadTitle({ to, from, next })
await checkLogin({ to, from, next })
await getPublicSetting({ to, from, next })
// console.log('Get public setting')
await getUserRoleAndSetRoutes({ to, from, next })
// console.log('Get profile')
return true
}

View File

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