107 lines
2.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 store from '@/store'
import { USER_STORE } from '@/store/constants'
2021-09-29 11:32:05 +02:00
import AddWorkout from '@/views/AddWorkout.vue'
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-09-23 16:45:57 +02:00
import NotFoundView from '@/views/NotFoundView.vue'
2021-10-13 17:29:12 +02:00
import ProfileView from '@/views/ProfileView.vue'
2021-10-03 19:23:17 +02:00
import StatisticsView from '@/views/StatisticsView.vue'
2021-10-05 15:34:31 +02:00
import EditWorkout from '@/views/workouts/EditWorkout.vue'
import Workout from '@/views/workouts/Workout.vue'
import Workouts from '@/views/workouts/WorkoutsView.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-10-13 17:29:12 +02:00
{
path: '/profile',
name: 'Profile',
component: ProfileView,
},
2021-10-03 19:23:17 +02:00
{
path: '/statistics',
name: 'Statistics',
component: StatisticsView,
},
2021-10-05 15:23:41 +02:00
{
path: '/workouts',
name: 'Workouts',
component: Workouts,
},
{
path: '/workouts/:workoutId',
name: 'Workout',
component: Workout,
2021-09-27 10:01:17 +02:00
props: { displaySegment: false },
},
2021-09-28 12:39:12 +02:00
{
path: '/workouts/:workoutId/edit',
name: 'EditWorkout',
component: EditWorkout,
},
2021-09-27 10:01:17 +02:00
{
path: '/workouts/:workoutId/segment/:segmentId',
name: 'WorkoutSegment',
component: Workout,
props: { displaySegment: true },
},
2021-09-29 11:32:05 +02:00
{
path: '/workouts/add',
name: 'AddWorkout',
component: AddWorkout,
},
2021-09-23 16:45:57 +02:00
{ path: '/:pathMatch(.*)*', name: 'not-found', component: NotFoundView },
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