connectionGuard.ts
32 lines
| 1 | import type { NavigationGuardNext, RouteLocationNormalized } from 'vue-router'; |
| 2 | |
| 3 | import { Route } from '@/types/enums/routeEnum'; |
| 4 | |
| 5 | const getTargetRoute = (isConnected: boolean) => (isConnected ? Route.Base.OVERVIEW : Route.Base.WELCOME); |
| 6 | |
| 7 | const shouldRedirect = (to: RouteLocationNormalized, isConnected: boolean) => { |
| 8 | if (to.path === '/') { |
| 9 | return getTargetRoute(isConnected); |
| 10 | } |
| 11 | |
| 12 | const targetRoute = getTargetRoute(isConnected); |
| 13 | if (to.name !== targetRoute) { |
| 14 | return targetRoute; |
| 15 | } |
| 16 | |
| 17 | return null; |
| 18 | }; |
| 19 | |
| 20 | export const connectionGuard = (to: RouteLocationNormalized, _: RouteLocationNormalized, next: NavigationGuardNext) => { |
| 21 | const isConnected = hostinger_reach_reach_data?.is_connected; |
| 22 | const redirectRoute = shouldRedirect(to, isConnected); |
| 23 | |
| 24 | if (redirectRoute) { |
| 25 | next({ name: redirectRoute }); |
| 26 | |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | next(); |
| 31 | }; |
| 32 |