feat: show the user menu as an avatar in the navbar #67

Merged
jfro merged 3 commits from jtk/navbar-avatar into main 2026-10-18 23:00:12 +00:00
Owner

The navbar user menu is now an avatar instead of the email.

  • New users.avatar_url column (migration 20261018220000_add_avatar_url_to_users).
  • On every OIDC login the avatar is synced from the picture claim: set, changed, or cleared when the IdP stops sending it. Only absolute http(s) URLs up to 2048 chars are kept (User.avatar_changeset/2), since it's used as an img src; anything else (javascript:, data:, relative) is dropped with a warning and never blocks the login. Changes are logged with the user id.
  • Layouts.user_avatar/1: a daisyUI avatar-placeholder circle with the first letter of the email, with the picture layered on top when there is one (so the letter still shows if the image fails). The img uses referrerpolicy="no-referrer". Users without OIDC get the letter.
  • Desktop: the menu button is the avatar (email in its title and at the top of the dropdown). Mobile: the avatar sits next to the email in the menu title.

Tests: picture sync (set, update, clear), bad picture claims, the avatar component with and without a picture, and the navbar rendering it. I checked the markup in tests but haven't looked at it in a browser, so it's worth a quick look on a dev server.

Fixes #49

The navbar user menu is now an avatar instead of the email. - New `users.avatar_url` column (migration `20261018220000_add_avatar_url_to_users`). - On every OIDC login the avatar is synced from the `picture` claim: set, changed, or cleared when the IdP stops sending it. Only absolute http(s) URLs up to 2048 chars are kept (`User.avatar_changeset/2`), since it's used as an img src; anything else (`javascript:`, `data:`, relative) is dropped with a warning and never blocks the login. Changes are logged with the user id. - `Layouts.user_avatar/1`: a daisyUI `avatar-placeholder` circle with the first letter of the email, with the picture layered on top when there is one (so the letter still shows if the image fails). The img uses `referrerpolicy="no-referrer"`. Users without OIDC get the letter. - Desktop: the menu button is the avatar (email in its title and at the top of the dropdown). Mobile: the avatar sits next to the email in the menu title. Tests: picture sync (set, update, clear), bad picture claims, the avatar component with and without a picture, and the navbar rendering it. I checked the markup in tests but haven't looked at it in a browser, so it's worth a quick look on a dev server. Fixes #49
jfro added this to the 0.6.2 milestone 2026-10-18 22:05:26 +00:00
feat: show the user menu as an avatar in the navbar
All checks were successful
CI / test (pull_request) Successful in 1m45s
b66657fa82
Author
Owner

Review from Nye (changes requested)

I did a static review of b66657f plus a render check: I rebuilt the avatar markup with the same classes against daisyUI 5 + Tailwind 4 in headless Chromium. CI is green.

Safety, all fine: avatar_changeset only keeps absolute http(s) URLs with a host, ≤ 2048 chars. javascript:, data:, relative paths and non-strings are dropped, and tests cover them. HEEx escapes the src attribute, so a URL can't inject markup. referrerpolicy=no-referrer is set. The avatar only shows in the user's own navbar, so a hostile picture claim can only make that user's own browser fetch it. The browser pipeline has no CSP, so external images load, and an http:// picture gets auto-upgraded by current browsers or falls back to the letter. The OIDC flow change is clean: sync_avatar runs only after a match or create, never blocks login, writes only when the value changed, and logs the id and set/cleared without the URL.

MEDIUM (blocking): the avatar doesn't render as a circle. daisyUI's .avatar > div rule (aspect-ratio 1/1, overflow hidden) and .avatar-placeholder > div rule (flex centering) only match a

child. user_avatar uses a , so neither applies. In Chromium the result is a 32×20 rounded pill with the letter left-aligned, and the picture is stretched to 32×20 with its square corners sticking out past the rounded edge. Fix: keep the span (a div isn't valid inside ) and add the classes yourself: class={["relative flex aspect-square items-center justify-center overflow-hidden rounded-full bg-primary text-primary-content", @class]}. I checked that this renders a 32×32 circle with the picture clipped round and the letter centered. Optionally add rounded-full object-cover on the img.

LOW: if the IdP later sends an invalid picture, sync_avatar logs it and keeps the OLD avatar_url. It should probably clear it to nil instead. The 'drops a bad claim' test only covers new users, so add a case where a user already has an avatar.

NIT: the new '# Keeps the avatar in step…' comment went in under the existing '# The role is always "user" here…' comment, so that create_oidc_user comment now sits above sync_avatar. Reorder them so each comment sits above its own function.

Checklist: conventional title, Fixes #49, tests, Logger context, no version bump, nullable text migration: all ok.

Review from Nye (changes requested) I did a static review of b66657f plus a render check: I rebuilt the avatar markup with the same classes against daisyUI 5 + Tailwind 4 in headless Chromium. CI is green. Safety, all fine: avatar_changeset only keeps absolute http(s) URLs with a host, ≤ 2048 chars. javascript:, data:, relative paths and non-strings are dropped, and tests cover them. HEEx escapes the src attribute, so a URL can't inject markup. referrerpolicy=no-referrer is set. The avatar only shows in the user's own navbar, so a hostile picture claim can only make that user's own browser fetch it. The browser pipeline has no CSP, so external images load, and an http:// picture gets auto-upgraded by current browsers or falls back to the letter. The OIDC flow change is clean: sync_avatar runs only after a match or create, never blocks login, writes only when the value changed, and logs the id and set/cleared without the URL. MEDIUM (blocking): the avatar doesn't render as a circle. daisyUI's `.avatar > div` rule (aspect-ratio 1/1, overflow hidden) and `.avatar-placeholder > div` rule (flex centering) only match a <div> child. user_avatar uses a <span>, so neither applies. In Chromium the result is a 32×20 rounded pill with the letter left-aligned, and the picture is stretched to 32×20 with its square corners sticking out past the rounded edge. Fix: keep the span (a div isn't valid inside <summary>) and add the classes yourself: class={["relative flex aspect-square items-center justify-center overflow-hidden rounded-full bg-primary text-primary-content", @class]}. I checked that this renders a 32×32 circle with the picture clipped round and the letter centered. Optionally add rounded-full object-cover on the img. LOW: if the IdP later sends an invalid picture, sync_avatar logs it and keeps the OLD avatar_url. It should probably clear it to nil instead. The 'drops a bad claim' test only covers new users, so add a case where a user already has an avatar. NIT: the new '# Keeps the avatar in step…' comment went in under the existing '# The role is always "user" here…' comment, so that create_oidc_user comment now sits above sync_avatar. Reorder them so each comment sits above its own function. Checklist: conventional title, Fixes #49, tests, Logger context, no version bump, nullable text migration: all ok.
fix: round navbar avatar and clear it on a bad picture claim
All checks were successful
CI / test (pull_request) Successful in 1m4s
1ed3ff3e93
The avatar span gets the circle classes itself (daisyUI's .avatar > div
rules don't match a span), the picture is clipped with object-cover, and
an invalid picture claim now clears the old avatar instead of keeping it.
Merge remote-tracking branch 'origin/main' into jtk/navbar-avatar
All checks were successful
CI / test (pull_request) Successful in 1m4s
c82cf45f62
# Conflicts:
#	lib/fuzzy_catalog/accounts/user.ex
#	test/fuzzy_catalog/accounts_test.exs
Author
Owner

Review from Nye (approved) at c82cf45

Covers 1ed3ff3 (span classes match the circle fix verified in headless Chromium; img classes add clipping; sync_avatar(user, nil) on an invalid claim can't loop since validate_change skips nil) and the main merge c82cf45. Resolution checked against git merge-tree of 1ed3ff3 vs main 2688fff: user.ex keeps both avatar_changeset and oidc_link_changeset with main's oidc_user? doc; accounts_test drops "never links to a local account" in favour of main's verified-email linking test. New test "a linked local account picks up the picture claim" is valid: every {:ok, user} from find_oidc_user (incl. link_oidc_identity) goes through sync_avatar. CI green.

Review from Nye (approved) at c82cf45 Covers 1ed3ff3 (span classes match the circle fix verified in headless Chromium; img classes add clipping; `sync_avatar(user, nil)` on an invalid claim can't loop since validate_change skips nil) and the main merge c82cf45. Resolution checked against `git merge-tree` of 1ed3ff3 vs main 2688fff: user.ex keeps both avatar_changeset and oidc_link_changeset with main's oidc_user? doc; accounts_test drops "never links to a local account" in favour of main's verified-email linking test. New test "a linked local account picks up the picture claim" is valid: every `{:ok, user}` from find_oidc_user (incl. link_oidc_identity) goes through sync_avatar. CI green.
jfro merged commit dc3391adf0 into main 2026-10-18 23:00:12 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jfro/fuzzy_catalog!67
No description provided.