MCPcopy Create free account
hub / github.com/dfinity/orbit / routeAccessGuard

Function routeAccessGuard

apps/wallet/src/plugins/router.plugin.ts:522–575  ·  view source on GitHub ↗
(to, _from, next)

Source from the content-addressed store, hash-verified

520});
521
522export 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
578router.beforeEach((_, _from, next) => {

Callers

nothing calls this directly

Calls 3

hasRequiredSessionFunction · 0.90
hasRequiredPrivilegeFunction · 0.90
toStringMethod · 0.80

Tested by

no test coverage detected