Repository navigation
feat(frontend): AuthGuard + JWT-derived role for /admin - #432
Merged
Merged
Conversation
The admin page had an ad-hoc in-component 403 check and derived role from the stored user object, not the JWT. Add a reusable guard and expose the role on the auth context: - `app/lib/auth.ts`: `readRole(token)` decodes the JWT payload's `role` (base64url, no signature verification), returning null for absent/malformed values — so it is a rendering hint, never authorisation. - `AuthContext`: expose `role` (from the JWT, falling back to the stored user), kept in sync across hydration/login/logout. - `app/components/AuthGuard.tsx`: client guard with `role: "admin" | "user"`. While auth is hydrating it renders a placeholder (never the children, never a premature redirect); once hydrated, a non-qualifying role is redirected to `/` with `router.replace` and a minimal notice so the admin UI never flashes. - `app/admin/page.tsx`: wrap the dashboard in `<AuthGuard role="admin">` and drop the bespoke 403 block. Tests: `readRole` unit tests (admin/user/malformed), an `AuthGuard` unit test (admin renders, non-admin redirects) and a Playwright spec (`e2e/admin-auth.spec.ts`) asserting a non-admin session is redirected away from `/admin`. Closes arflexx#284
|
@Zuliyat Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #284
The admin page had an ad-hoc, in-component 403 check and derived role from the stored user object rather than the JWT. This adds a reusable guard and exposes the role on the auth context. (Note: a
role !== "admin"guard already existed onmain— deliberately a UX affordance, not a security control — so this is the acceptance's AuthGuard/redirect/rolework on top of it.)Changes
app/lib/auth.ts—readRole(token)decodes the JWT payload'srole(base64url, no signature verification); absent/malformed ⇒null(treated as non-admin). Documented as a rendering hint, never authorisation.AuthContext— newrolefield (from the JWT, falling back to the stored user), kept in sync across hydration/login/logout.app/components/AuthGuard.tsx— client guard{ role: "admin" | "user"; children }. During hydration it renders a placeholder (never children, never a premature redirect); once hydrated a non-qualifying role isrouter.replace("/")d with a minimal notice so the admin UI never flashes.app/admin/page.tsx— dashboard wrapped in<AuthGuard role="admin">; the bespoke 403 block removed; header comment updated to point at the guard.Tests
readRole(admin/user/malformed) andAuthGuard(admin renders children; non-admin redirects to/).e2e/admin-auth.spec.ts— a non-admin session is redirected away from/adminto/and never sees the dashboard. Uses the existingsignIn(page, { role })mock helper. (Added here; it needs the app + stub API servers to run, which aren't available in this environment.)npx tsc --noEmit→ exit 0.