2017-08-21 07:55:46 +00:00
|
|
|
import router from './router'
|
|
|
|
import store from './store'
|
2018-01-05 03:38:34 +00:00
|
|
|
import { Message } from 'element-ui'
|
2017-12-29 08:07:42 +00:00
|
|
|
import NProgress from 'nprogress' // progress bar
|
|
|
|
import 'nprogress/nprogress.css'// progress bar style
|
|
|
|
import { getToken } from '@/utils/auth' // getToken from cookie
|
2017-08-21 07:55:46 +00:00
|
|
|
|
2018-01-05 03:38:34 +00:00
|
|
|
NProgress.configure({ showSpinner: false })// NProgress Configuration
|
|
|
|
|
2018-06-07 05:26:10 +00:00
|
|
|
// permission judge function
|
2017-08-21 07:55:46 +00:00
|
|
|
function hasPermission(roles, permissionRoles) {
|
2017-12-29 08:07:42 +00:00
|
|
|
if (roles.indexOf('admin') >= 0) return true // admin permission passed directly
|
2017-08-21 07:55:46 +00:00
|
|
|
if (!permissionRoles) return true
|
|
|
|
return roles.some(role => permissionRoles.indexOf(role) >= 0)
|
|
|
|
}
|
|
|
|
|
2019-09-13 20:50:21 +00:00
|
|
|
const whiteList = ['/login', '/auth-redirect', '/login-pleroma']// no redirect whitelist
|
2017-11-17 09:51:41 +00:00
|
|
|
|
2019-03-23 14:09:48 +00:00
|
|
|
export const beforeEachRoute = (to, from, next) => {
|
2017-12-29 08:07:42 +00:00
|
|
|
NProgress.start() // start progress bar
|
2018-01-05 03:38:34 +00:00
|
|
|
if (getToken()) { // determine if there has token
|
|
|
|
/* has token*/
|
2017-08-21 07:55:46 +00:00
|
|
|
if (to.path === '/login') {
|
|
|
|
next({ path: '/' })
|
2018-01-05 03:38:34 +00:00
|
|
|
NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
|
2017-08-21 07:55:46 +00:00
|
|
|
} else {
|
2019-03-23 14:09:48 +00:00
|
|
|
if (store.getters.roles.length === 0) {
|
|
|
|
store.dispatch('GetUserInfo').then(res => {
|
2019-05-21 19:24:48 +00:00
|
|
|
const roles = res.data.pleroma.is_admin ? ['admin'] : []
|
2019-03-23 14:09:48 +00:00
|
|
|
store.dispatch('GenerateRoutes', { roles }).then(() => {
|
|
|
|
router.addRoutes(store.getters.addRouters)
|
|
|
|
next({ ...to, replace: true })
|
2017-08-21 07:55:46 +00:00
|
|
|
})
|
2018-05-02 09:37:47 +00:00
|
|
|
}).catch((err) => {
|
2017-08-21 07:55:46 +00:00
|
|
|
store.dispatch('FedLogOut').then(() => {
|
2020-03-26 19:29:41 +00:00
|
|
|
Message({
|
2020-03-29 19:21:09 +00:00
|
|
|
dangerouslyUseHTMLString: true,
|
2020-03-26 19:29:41 +00:00
|
|
|
message: err,
|
|
|
|
type: 'error',
|
2020-03-29 19:52:32 +00:00
|
|
|
duration: 10 * 1000
|
2020-03-26 19:29:41 +00:00
|
|
|
})
|
2018-05-02 09:37:47 +00:00
|
|
|
next({ path: '/' })
|
2017-08-21 07:55:46 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
2018-01-05 03:38:34 +00:00
|
|
|
if (hasPermission(store.getters.roles, to.meta.roles)) {
|
2018-08-15 09:29:15 +00:00
|
|
|
next()
|
2017-08-21 07:55:46 +00:00
|
|
|
} else {
|
2018-01-05 03:38:34 +00:00
|
|
|
next({ path: '/401', replace: true, query: { noGoBack: true }})
|
2017-08-21 07:55:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-05 03:38:34 +00:00
|
|
|
/* has no token*/
|
2019-03-23 14:09:48 +00:00
|
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
2017-08-21 07:55:46 +00:00
|
|
|
next()
|
|
|
|
} else {
|
2019-03-23 14:09:48 +00:00
|
|
|
next(`/login?redirect=${to.path}`)
|
2018-01-05 03:38:34 +00:00
|
|
|
NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
|
2017-08-21 07:55:46 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-23 14:09:48 +00:00
|
|
|
}
|
|
|
|
router.beforeEach(beforeEachRoute)
|
2017-08-21 07:55:46 +00:00
|
|
|
|
|
|
|
router.afterEach(() => {
|
2017-12-29 08:07:42 +00:00
|
|
|
NProgress.done() // finish progress bar
|
2017-08-21 07:55:46 +00:00
|
|
|
})
|