Repository navigation
fix(auth): accept user.id in music context and lock screen - #178
Conversation
Ziqian-Huang0607
left a comment
There was a problem hiding this comment.
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.
dailker
left a comment
There was a problem hiding this comment.
Thanks @chojuninengu, this is a clean fix and the write-up made it quick to check.
I verified the claims:
GET /api/auth/me,loginandregisterall serializeid: user._id, souser?._idreally wasundefinedfor every real session. After this PR,LockScreenandMusicContextare 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.
Closes #174
What and why
GET /api/auth/me,POST /api/auth/login, andPOST /api/auth/registerserialize the signed-in user's identifier asuser.id(without_id), whereasMusicContextandLockScreenonly readuser?._id.Because of that mismatch:
userId = user?._id || nullasnullfor every real MongoDB session, skipping both preference restoration and debouncedPUT /api/users/preferencessaves.POST /api/auth/local-admininitially returns both_idandid, so unlocking worked before reload, but after reloadGET /api/auth/mereturns onlyid, causinguser?._id === LOCAL_ADMIN_IDto becomefalseand rendering a required password field for the passwordless Local Admin.This PR updates
MusicContextandLockScreento readuser?.id || user?._id(matchingLauncher.jsanduseDesktopLayout.js) and updatesLockScreenfocus management whenisLocalAdminresolves asynchronously after session hydration.How I tested it
tests/MusicPersistence.test.js/api/auth/meand/api/auth/loginmocks 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 intests/LockScreen.test.js.main(8 failedacrossMusicPersistence.test.jsandLockScreen.test.js).npm test→ 35 passed, 35 total (418 tests passed)npm run lint→ 0 errorsnpx prettier --check src/context/MusicContext.js src/components/LockScreen.js tests/MusicPersistence.test.js tests/LockScreen.test.js→ All matchednpm run build→ Compiled successfullyChecklist
type(scope): summary, for examplefix(calculator): keep history after reloadnpm testpassesnpx prettier --checkpasses on the files I changed