Front-end auth improvements
Former-commit-id: 068e447a6332149f7c621da924100bacc5a02752 [formerly 80b5e008e56b9c48ccc0450effabc7f33dfd20b4] [formerly 0f8b405cb136355711970a9d9f3f1210272101ca [formerly 85e01a67c3f9e33ad0ac7fef5eeab612f747c08b]] Former-commit-id: 03a1448741f695e2d5c681a11d9dcdca750ff61d [formerly 218dd8e95058a02cafee341dc2330c0a700972df] Former-commit-id: 0d7ebe389f3e640f6a0102e8a4b5a28e395842c0
This commit is contained in:
72
_assets/src/router/index.js
Normal file
72
_assets/src/router/index.js
Normal file
@@ -0,0 +1,72 @@
|
||||
import Vue from 'vue'
|
||||
import Router from 'vue-router'
|
||||
import Login from '@/components/Login'
|
||||
import Files from '@/components/Files'
|
||||
import Main from '@/components/Main'
|
||||
import auth from '@/utils/auth.js'
|
||||
|
||||
Vue.use(Router)
|
||||
|
||||
const router = new Router({
|
||||
base: document.querySelector('meta[name="base"]').getAttribute('content'),
|
||||
mode: 'history',
|
||||
routes: [
|
||||
{
|
||||
path: '/login',
|
||||
name: 'Login',
|
||||
component: Login,
|
||||
beforeEnter: function (to, from, next) {
|
||||
auth.loggedIn()
|
||||
.then(() => {
|
||||
next({ path: '/files' })
|
||||
})
|
||||
.catch(() => {
|
||||
next()
|
||||
})
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/*',
|
||||
component: Main,
|
||||
meta: {
|
||||
requiresAuth: true
|
||||
},
|
||||
children: [
|
||||
{
|
||||
path: '/files/*',
|
||||
name: 'Files',
|
||||
component: Files
|
||||
},
|
||||
{
|
||||
path: '/*',
|
||||
redirect: {
|
||||
name: 'Files'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
if (to.matched.some(record => record.meta.requiresAuth)) {
|
||||
// this route requires auth, check if logged in
|
||||
// if not, redirect to login page.
|
||||
auth.loggedIn()
|
||||
.then(() => {
|
||||
next()
|
||||
})
|
||||
.catch(e => {
|
||||
next({
|
||||
path: '/login',
|
||||
query: { redirect: to.fullPath }
|
||||
})
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
export default router
|
||||
Reference in New Issue
Block a user