FitTrackee/fittrackee_client/src/router/index.ts

64 lines
1.5 KiB
TypeScript
Raw Normal View History

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-14 19:43:19 +02:00
import LoginOrRegister from '@/views/LoginOrRegister.vue'
2021-08-07 14:28:48 +02:00
import NotFound from '@/views/NotFound.vue'
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-08-08 11:49:01 +02:00
{
path: '/login',
name: 'Login',
2021-08-14 19:43:19 +02:00
component: LoginOrRegister,
props: { action: 'login' },
},
{
path: '/register',
name: 'Register',
component: LoginOrRegister,
props: { action: 'register' },
2021-08-08 11:49:01 +02:00
},
2021-08-07 14:28:48 +02:00
{ path: '/:pathMatch(.*)*', name: 'not-found', component: NotFound },
2021-07-25 13:23:25 +02:00
]
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes,
2021-07-25 13:23:25 +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] &&
2021-08-14 19:43:19 +02:00
['/login', '/register'].includes(to.path)
2021-08-14 19:24:19 +02:00
) {
return next('/')
} else if (
!store.getters[USER_STORE.GETTERS.IS_AUTHENTICATED] &&
2021-08-14 19:43:19 +02:00
!['/login', '/register'].includes(to.path)
2021-08-14 19:24:19 +02:00
) {
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