2019-04-15 16:50:38 +02:00
|
|
|
import Vue from 'vue'
|
|
|
|
import Router from 'vue-router'
|
2020-05-22 11:41:48 +02:00
|
|
|
import Dashboard from '@/views/Dashboard.vue'
|
|
|
|
import Login from '@/views/Login.vue'
|
2020-05-23 10:13:26 +02:00
|
|
|
import {isAuthenticated} from '@/services/auth.js'
|
2019-04-15 16:50:38 +02:00
|
|
|
|
|
|
|
Vue.use(Router)
|
|
|
|
|
2020-05-22 11:41:48 +02:00
|
|
|
const router = new Router({
|
2020-05-20 14:49:42 +02:00
|
|
|
mode: 'history',
|
|
|
|
base: process.env.BASE_URL,
|
|
|
|
routes: [
|
|
|
|
{
|
|
|
|
path: '/',
|
|
|
|
name: 'dashboard',
|
|
|
|
component: Dashboard
|
2020-05-22 11:41:48 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '/login',
|
|
|
|
name: 'login',
|
|
|
|
component: Login,
|
|
|
|
meta: {
|
|
|
|
public: true, // Allow access to even if not logged in
|
|
|
|
onlyWhenLoggedOut: true
|
|
|
|
}
|
2020-05-20 14:49:42 +02:00
|
|
|
}
|
|
|
|
]
|
2019-04-15 16:50:38 +02:00
|
|
|
})
|
2020-05-22 11:41:48 +02:00
|
|
|
|
|
|
|
|
2020-05-23 10:13:26 +02:00
|
|
|
router.beforeEach(async(to, from, next) => {
|
2020-05-22 11:41:48 +02:00
|
|
|
const isPublic = to.matched.some(record => record.meta.public)
|
|
|
|
const onlyWhenLoggedOut = to.matched.some(record => record.meta.onlyWhenLoggedOut)
|
2020-05-23 10:13:26 +02:00
|
|
|
const authenticated = await isAuthenticated();
|
2020-05-22 11:41:48 +02:00
|
|
|
|
2020-05-23 10:13:26 +02:00
|
|
|
if (!isPublic && !authenticated) {
|
2020-05-22 11:41:48 +02:00
|
|
|
return next({
|
|
|
|
path:'/login',
|
|
|
|
query: {redirect: to.fullPath} // Store the full path to redirect the user to after login
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not allow user to visit login page or register page if they are logged in
|
2020-05-23 10:13:26 +02:00
|
|
|
if (authenticated && onlyWhenLoggedOut) {
|
2020-05-22 11:41:48 +02:00
|
|
|
return next('/')
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
})
|
|
|
|
|
|
|
|
export default router;
|