2021-07-25 13:23:25 +02:00
|
|
|
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
|
2021-08-11 22:21:26 +02:00
|
|
|
|
2021-08-14 19:24:19 +02:00
|
|
|
import { USER_STORE } from '@/store/constants'
|
|
|
|
import store from '@/store'
|
2021-07-25 13:34:44 +02:00
|
|
|
import Dashboard from '@/views/DashBoard.vue'
|
2021-08-08 11:49:01 +02:00
|
|
|
import Login from '@/views/Login.vue'
|
2021-08-07 14:28:48 +02:00
|
|
|
import NotFound from '@/views/NotFound.vue'
|
2021-07-24 20:56:37 +02:00
|
|
|
|
|
|
|
const routes: Array<RouteRecordRaw> = [
|
|
|
|
{
|
2021-07-25 13:23:25 +02:00
|
|
|
path: '/',
|
2021-07-25 13:34:44 +02:00
|
|
|
name: 'Dashboard',
|
|
|
|
component: Dashboard,
|
2021-07-24 20:56:37 +02:00
|
|
|
},
|
2021-08-08 11:49:01 +02:00
|
|
|
{
|
|
|
|
path: '/login',
|
|
|
|
name: 'Login',
|
|
|
|
component: Login,
|
|
|
|
},
|
2021-08-07 14:28:48 +02:00
|
|
|
{ path: '/:pathMatch(.*)*', name: 'not-found', component: NotFound },
|
2021-07-25 13:23:25 +02:00
|
|
|
]
|
2021-07-24 20:56:37 +02:00
|
|
|
|
|
|
|
const router = createRouter({
|
|
|
|
history: createWebHistory(process.env.BASE_URL),
|
|
|
|
routes,
|
2021-07-25 13:23:25 +02:00
|
|
|
})
|
2021-07-24 20:56:37 +02:00
|
|
|
|
2021-08-14 19:24:19 +02:00
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
|
store
|
|
|
|
.dispatch(USER_STORE.ACTIONS.CHECK_AUTH_USER)
|
|
|
|
.then(() => {
|
|
|
|
if (
|
|
|
|
store.getters[USER_STORE.GETTERS.IS_AUTHENTICATED] &&
|
|
|
|
['/login'].includes(to.path)
|
|
|
|
) {
|
|
|
|
return next('/')
|
|
|
|
} else if (
|
|
|
|
!store.getters[USER_STORE.GETTERS.IS_AUTHENTICATED] &&
|
|
|
|
!['/login'].includes(to.path)
|
|
|
|
) {
|
|
|
|
const path =
|
|
|
|
to.path === '/'
|
|
|
|
? { path: '/login' }
|
|
|
|
: { path: '/login', query: { from: to.fullPath } }
|
|
|
|
next(path)
|
|
|
|
} else {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(error)
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-07-25 13:23:25 +02:00
|
|
|
export default router
|