Skip to content

fix(auth): accept user.id in music context and lock screen - #178

Merged
Ziqian-Huang0607 merged 1 commit into
codehubbers:mainfrom
chojuninengu:fix/user-id-session-shape
Dec 3, 2026
Merged

Ziqian-Huang0607 merged 1 commit into
codehubbers:mainfrom
chojuninengu:fix/user-id-session-shape

Conversation

@chojuninengu

Copy link
Copy Markdown
Member

Closes #174

What and why

GET /api/auth/me, POST /api/auth/login, and POST /api/auth/register serialize the signed-in user's identifier as user.id (without _id), whereas MusicContext and LockScreen only read user?._id.

Because of that mismatch:

  1. Music Player playlist persistence (Music Player: keep the playlist after a page reload #127) evaluated userId = user?._id || null as null for every real MongoDB session, skipping both preference restoration and debounced PUT /api/users/preferences saves.
  2. Local Admin lock screen (🔒 Lock screen and idle session timeout #98) broke after a page reload: POST /api/auth/local-admin initially returns both _id and id, so unlocking worked before reload, but after reload GET /api/auth/me returns only id, causing user?._id === LOCAL_ADMIN_ID to become false and rendering a required password field for the passwordless Local Admin.

This PR updates MusicContext and LockScreen to read user?.id || user?._id (matching Launcher.js and useDesktopLayout.js) and updates LockScreen focus management when isLocalAdmin resolves asynchronously after session hydration.

How I tested it

  1. Updated tests/MusicPersistence.test.js /api/auth/me and /api/auth/login mocks to return { id: ... } (matching the real API payload, plus a fallback test for _id) and added a post-reload { id: LOCAL_ADMIN_ID } hydration test in tests/LockScreen.test.js.
  2. Verified the tests failed against unpatched main (8 failed across MusicPersistence.test.js and LockScreen.test.js).
  3. Applied the fix and ran the full local check suite:
    • npm test → 35 passed, 35 total (418 tests passed)
    • npm run lint → 0 errors
    • npx prettier --check src/context/MusicContext.js src/components/LockScreen.js tests/MusicPersistence.test.js tests/LockScreen.test.js → All matched
    • npm run build → Compiled successfully

Checklist

  • I read CONTRIBUTING.md
  • Commits follow type(scope): summary, for example fix(calculator): keep history after reload
  • Tests added or updated, and npm test passes
  • npx prettier --check passes on the files I changed
  • CI is green

@Ziqian-Huang0607 Ziqian-Huang0607 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice catch, and verified independently.

I confirmed the root cause against the API code: me.js, login.js and register.js all serialize the user as id: user._id with no _id key, so every real session had user?._id === undefined client-side. That means Music Player persistence was silently broken for all signed-in users (restores and saves skipped), and the local admin lock screen regressed after a reload exactly as described.

I also grepped the whole client for user?._id / user._id reads: after this PR there are none left. Launcher and useDesktopLayout already use the dual pattern, and the remaining _id uses are all server side (JWT payload / Mongoose documents), where they are correct.

The focus fix is right too: with [] the focus stayed on the password input after hydration flipped isLocalAdmin to true; depending on [isLocalAdmin] refocuses the Unlock button, and the new test pins that.

Ran the patched code locally: LockScreen + MusicPersistence suites, 42/42 green. CI green.

One thought for a follow-up, not for this PR: the real fix for the whole class of bugs is to make the API return both id and _id (or one canonical serialization helper), so the next component does not have to remember the dual-read pattern.

Approve from me.

@MXD-K1 MXD-K1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good.

@dailker dailker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @chojuninengu, this is a clean fix and the write-up made it quick to check.

I verified the claims:

  • GET /api/auth/me, login and register all serialize id: user._id, so user?._id really was undefined for every real session. After this PR, LockScreen and MusicContext are the only two client-side readers of _id, and both are fixed.
  • I ran your tests against main's versions of the two source files and got exactly 8 failed. On this branch: 42/42 pass. CI is green.
  • Swapping the one-shot focus effect for [isLocalAdmin] is the right call. It also removes an eslint-disable.

A non-blocking nit: there are now four spellings of "the user's id" (Launcher and useDesktopLayout check _id first, this PR checks id first). A tiny getUserId(user) in src/lib would stop the next component getting it wrong. That's a good follow-up, so no need to do it here.

@dailker dailker added the bug Something isn't working label Dec 3, 2026
@dailker
dailker self-requested a review December 3, 2026 12:03
@dailker dailker added the frontend UI / React / Next.js pages label Dec 3, 2026
@Ziqian-Huang0607
Ziqian-Huang0607 merged commit ad7f7b6 into codehubbers:main Dec 3, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working frontend UI / React / Next.js pages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Music playlist persistence and Local Admin lock screen fail due to user.id vs user._id mismatch

4 participants