Route Guard redirect logic for global admin to No-Access
epic-role-based-access-control-ui-and-navigation-task-011 — Implement specific redirect logic within the Route Guard to detect global admin role and unconditionally redirect to the No-Access Screen. Ensure this redirect fires on deep link navigation, app resume, and standard navigation events. Write unit tests covering global admin redirect, coordinator access, peer mentor access, and unauthenticated access.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 5 - 253 tasks
Can start after Tier 4 completes
Implementation Notes
Extract the guard logic into a pure static method RoleRouteGuard.evaluate(AppRole? role, String targetPath) → String? for easy unit testing without Flutter widget context. The GoRouter redirect callback simply calls this method.
To handle app resume correctly, pass a refreshListenable to GoRouter that listens to both the auth state stream (Supabase onAuthStateChange) and the role state stream — any emission triggers guard re-evaluation. Use an enum guard clause: if (role == AppRole.globalAdmin && path != '/no-access') return '/no-access'. Always check for exempt routes (login, no-access, splash) before applying role checks to prevent infinite redirect loops. The No-Access Screen should call Supabase.instance.client.auth.signOut() on the logout button tap and navigate to '/login'.
Testing Requirements
Unit tests (flutter_test, mocktail for mocking): (1) RoleRouteGuard.evaluate(AppRole.globalAdmin, '/home') == '/no-access', (2) RoleRouteGuard.evaluate(AppRole.globalAdmin, '/no-access') == null (no loop), (3) RoleRouteGuard.evaluate(null, '/home') == '/login', (4) RoleRouteGuard.evaluate(AppRole.coordinator, '/home') == null, (5) RoleRouteGuard.evaluate(AppRole.peerMentor, '/coordinator-only') == '/home'. Widget test: render No-Access Screen and assert message text and logout button are present and accessible. Integration test: configure full GoRouter with guard, set globalAdmin role in provider, navigate to '/home', assert route is '/no-access'. Test app resume scenario by simulating GoRouter refreshListenable notification with globalAdmin role.
Target 100% line coverage on guard redirect method.
Combining GoRouter's declarative redirect logic in the route guard with StatefulShellRoute's stateful branch management is known to produce subtle bugs where the shell rebuilds unnecessarily on role switches, losing tab state or causing double-navigation events.
Mitigation & Contingency
Mitigation: Implement the route guard as a GoRouter redirect callback that only evaluates role from an already-resolved Riverpod provider (not async). Use a dedicated ShellRoute navigator key per tab branch to anchor state independently of role-driven rebuilds. Write integration tests for the full navigation graph.
Contingency: If StatefulShellRoute state loss is confirmed during QA, fall back to a manual tab state preservation approach using a TabStateManager service that caches the last route per tab and restores it after role switches, decoupling tab state from the shell lifecycle.
The role-based home screen must render three significantly different layouts (coordinator dashboard, peer mentor activity summary, org admin overview). If these variants are implemented as a single widget with conditionals, the file will become unmaintainable and difficult to test in isolation, especially as each variant grows with downstream feature additions.
Mitigation & Contingency
Mitigation: Design the role-based home screen as a router/dispatcher widget that delegates to three separate variant widgets (CoordinatorHomeView, PeerMentorHomeView, OrgAdminHomeView). Each variant is independently testable and can be developed by separate team members in parallel.
Contingency: If variant coupling has already occurred before this risk is addressed, refactor to the dispatcher pattern in a dedicated cleanup task before feature handoff. The dispatcher pattern is a straightforward extraction that carries low refactoring risk.
The no-access screen must link global admin users to the correct admin portal URL, which may differ per organization (NHF, HLF, Blindeforbundet each have their own admin portals). Hardcoding a single URL will result in wrong or broken links for some global admin users.
Mitigation & Contingency
Mitigation: Source the admin portal URL from the organization's configuration record in Supabase rather than hardcoding it. The no-access screen reads the active org context and resolves the portal URL dynamically. Provide a safe fallback to a generic Norse Digital Products support page if the URL is not configured.
Contingency: If dynamic URL resolution is not ready when the no-access screen ships, display a static instruction to contact the organization's administrator along with a support email address as an interim measure, and track the URL configuration task as a follow-up.