Skip to content

Add Cognito groups to auth context and user profile - #44

Merged
raylrui merged 3 commits into
mainfrom
feat/add-user-groups-info
Aug 18, 2026
Merged

raylrui merged 3 commits into
mainfrom
feat/add-user-groups-info

Conversation

@raylrui

@raylrui raylrui commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

What

Surfaces the cognito:groups JWT claim in the auth context and displays it in the User Profile modal.

Why

Group membership was only accessible by manually decoding the JWT token. Exposing it via the auth context makes it available app-wide for consistent display and future RBAC use.

Changes

Auth provider (AmplifyAuthProvider.tsx)

  • Calls fetchAuthSession() in parallel with fetchUserAttributes() during auth initialisation (reads from local cache — no extra network round-trip)
  • Extracts cognito:groups from the ID token payload
  • Normalises autogenerated Cognito IDP group names (e.g. ap-southeast-2_iWOHnsurL_Google → Google (default)) before storing in context, so all consumers receive friendly names

Auth context (auth-context.ts)

  • Adds groups: string[] to AuthContextValue

User Profile modal (UserProfileModal.tsx)

  • Accepts groups prop and renders a Groups section as pill badges (hidden when empty)

User menu (UserMenu.tsx)

  • Passes groups from useAuthContext() to UserProfileModal

Notes

  • Normalisation regex: /^[a-z]+-[a-z]+-\d+_[A-Za-z0-9]+_(\w+)$/ — matches the {region}_{poolId}_{Provider} Cognito pattern and renders the provider suffix with (default) label
  • Non-IDP groups (e.g. admin, bioinfo, curators) pass through unchanged

@raylrui raylrui self-assigned this Aug 18, 2026
@raylrui raylrui added the enhancement New feature or request label Aug 18, 2026
@raylrui
raylrui requested a lite review from Copilot August 18, 2026 06:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request exposes Cognito group membership (cognito:groups from the ID token) via the app’s auth context and surfaces it in the User Profile modal so group info is available consistently across the UI (and for future RBAC usage).

Changes:

  • Adds groups: string[] to the AuthContextValue and auth provider state, populated from fetchAuthSession() during initialization.
  • Normalizes Cognito’s autogenerated federated-IDP group names into friendlier labels before storing them in context.
  • Displays groups in the User Profile modal and wires the prop through UserMenu; updates existing layout tests’ auth mocks.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/context/auth-context.ts Extends the auth context value type to include groups: string[].
src/context/AmplifyAuthProvider.tsx Fetches auth session alongside user attributes, extracts/normalizes cognito:groups, and stores it in provider state/context.
src/components/layout/header/UserProfileModal.tsx Adds optional groups prop and renders a Groups section with pill badges when non-empty.
src/components/layout/header/UserMenu.tsx Passes groups from useAuthContext() into UserProfileModal.
src/components/layout/tests/Header.test.tsx Updates AuthContextValue test mock to include groups.
src/components/layout/tests/app-shell.test.tsx Updates AuthContextValue test mock to include groups.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +131 to +151
{/* Groups */}
{groups.length > 0 && (
<div>
<p className='mb-2 text-[11px] font-semibold tracking-wider text-slate-400 uppercase dark:text-neutral-500'>
Groups
</p>
<div className='flex items-start gap-3 rounded-lg border border-slate-200 px-4 py-3 dark:border-[#2d3540]'>
<Users className='mt-0.5 h-4 w-4 shrink-0 text-slate-400 dark:text-[#9dabb9]' />
<div className='flex min-w-0 flex-1 flex-wrap gap-1.5'>
{groups.map((g) => (
<span
key={g}
className='inline-flex items-center rounded-full bg-slate-100 px-2.5 py-0.5 text-[11px] font-medium text-slate-700 dark:bg-[#2d3540] dark:text-slate-300'
>
{g}
</span>
))}
</div>
</div>
</div>
)}
@raylrui
raylrui merged commit 10d69cb into main Aug 18, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants