(to, _from, next)
| 520 | }); |
| 521 | |
| 522 | export const routeAccessGuard: NavigationGuard = async (to, _from, next) => { |
| 523 | const session = useSessionStore(); |
| 524 | const app = useAppStore(); |
| 525 | |
| 526 | if (to.name === Routes.Disconnected && session.data.selected.hasAccess) { |
| 527 | return next({ name: defaultHomeRoute }); |
| 528 | } |
| 529 | |
| 530 | if (to.name === Routes.Initialization && (!session.isAuthenticated || session.hasStations)) { |
| 531 | return next({ name: defaultHomeRoute }); |
| 532 | } |
| 533 | |
| 534 | if ( |
| 535 | to.name && |
| 536 | ![Routes.Initialization.toString(), Routes.MySettings.toString()].includes( |
| 537 | to.name.toString(), |
| 538 | ) && |
| 539 | session.isAuthenticated && |
| 540 | !session.hasStations |
| 541 | ) { |
| 542 | return next({ name: Routes.Initialization }); |
| 543 | } |
| 544 | |
| 545 | const matchesRequiredSession = hasRequiredSession(to.meta.auth.check.session); |
| 546 | if (!matchesRequiredSession) { |
| 547 | switch (to.meta.auth.check.session) { |
| 548 | case RequiredSessionState.Authenticated: |
| 549 | // save the current route to redirect back after login |
| 550 | sessionStorage.setItem(redirectToKey, to.fullPath); |
| 551 | return next({ name: defaultLoginRoute }); |
| 552 | case RequiredSessionState.ConnectedToStation: { |
| 553 | if (!session.isAuthenticated) { |
| 554 | // save the current route to redirect back after login |
| 555 | sessionStorage.setItem(redirectToKey, to.fullPath); |
| 556 | return next({ name: defaultLoginRoute }); |
| 557 | } |
| 558 | |
| 559 | app.routeStatusCode = RouteStatusCode.Disconnected; |
| 560 | return next(); |
| 561 | } |
| 562 | default: { |
| 563 | return next({ name: defaultHomeRoute }); |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | const matchesRequiredPrivilege = hasRequiredPrivilege({ anyOf: to.meta.auth.check.privileges }); |
| 569 | if (!matchesRequiredPrivilege) { |
| 570 | app.routeStatusCode = RouteStatusCode.Unauthorized; |
| 571 | return next(); |
| 572 | } |
| 573 | |
| 574 | return next(); |
| 575 | }; |
| 576 | |
| 577 | // needs to be the first guard to begin the loading state of the app routing |
| 578 | router.beforeEach((_, _from, next) => { |
nothing calls this directly
no test coverage detected