diff --git a/src/components/LockScreen.js b/src/components/LockScreen.js index 90e339f..c8d4c88 100644 --- a/src/components/LockScreen.js +++ b/src/components/LockScreen.js @@ -27,7 +27,7 @@ export default function LockScreen({ user, wallpaper, onUnlock }) { const inputRef = useRef(null); const buttonRef = useRef(null); - const isLocalAdmin = user?._id === LOCAL_ADMIN_ID; + const isLocalAdmin = (user?.id || user?._id) === LOCAL_ADMIN_ID; useEffect(() => { const timer = setInterval(() => setNow(new Date()), 1000); @@ -36,8 +36,7 @@ export default function LockScreen({ user, wallpaper, onUnlock }) { useEffect(() => { (isLocalAdmin ? buttonRef : inputRef).current?.focus(); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); + }, [isLocalAdmin]); const name = user?.displayName || user?.username || 'User'; diff --git a/src/context/MusicContext.js b/src/context/MusicContext.js index 8a7dd38..2ddf87b 100644 --- a/src/context/MusicContext.js +++ b/src/context/MusicContext.js @@ -311,7 +311,7 @@ export function MusicProvider({ children }) { // changes, and a sign-out or account switch must never leak user A's // playlist into user B's preferences. const { user, isLoading: authLoading } = useAuth(); - const userId = user?._id || null; + const userId = user?.id || user?._id || null; const restoreStateRef = useRef('pending'); // 'pending' | 'done' const basePreferencesRef = useRef(null); // null until we know the user diff --git a/tests/LockScreen.test.js b/tests/LockScreen.test.js index 4e8bb3c..253ba2d 100644 --- a/tests/LockScreen.test.js +++ b/tests/LockScreen.test.js @@ -365,6 +365,35 @@ describe('LockScreen', () => { expect.objectContaining({ body: JSON.stringify({}) }), ); }); + + it('recognises the local dev admin after a reload when /api/auth/me returns user.id without _id', async () => { + global.fetch = jest.fn().mockResolvedValue({ ok: true, status: 200 }); + const onUnlock = jest.fn(); + const { rerender } = render(); + + // Hydrates asynchronously from GET /api/auth/me, which returns `id` (no `_id`). + rerender( + , + ); + + expect(screen.queryByLabelText('Password')).not.toBeInTheDocument(); + const button = screen.getByRole('button', { name: 'Unlock' }); + expect(button).toHaveFocus(); + fireEvent.click(button); + + await waitFor(() => expect(onUnlock).toHaveBeenCalled()); + expect(global.fetch).toHaveBeenCalledWith( + '/api/auth/verify-password', + expect.objectContaining({ body: JSON.stringify({}) }), + ); + }); }); describe('useLockScreen', () => { diff --git a/tests/MusicPersistence.test.js b/tests/MusicPersistence.test.js index d1cf284..5532526 100644 --- a/tests/MusicPersistence.test.js +++ b/tests/MusicPersistence.test.js @@ -92,7 +92,7 @@ function mockFetch({ return { ok: meStatus === 200, status: meStatus, - json: async () => ({ user: { _id: 'user-1', preferences } }), + json: async () => ({ user: { id: 'user-1', preferences } }), }; } if (url === '/api/files?meta=1') { @@ -115,8 +115,8 @@ function mockFetch({ // /api/auth/me reports. function mockAuthFlow() { const puts = []; - const USER_A = { _id: 'user-a', preferences: { music: savedMusic() } }; - const USER_B = { _id: 'user-b', preferences: {} }; + const USER_A = { id: 'user-a', preferences: { music: savedMusic() } }; + const USER_B = { id: 'user-b', preferences: {} }; let currentUser = null; global.fetch = jest.fn(async (url, options) => { @@ -322,7 +322,7 @@ describe('Music Player playlist persistence (#127)', () => { ok: true, status: 200, json: async () => ({ - user: { _id: 'user-1', preferences: { music: savedMusic() } }, + user: { id: 'user-1', preferences: { music: savedMusic() } }, }), }; } @@ -409,4 +409,33 @@ describe('Music Player playlist persistence (#127)', () => { const lastPut = puts[puts.length - 1]; expect(lastPut.preferences.music.trackIds).toEqual(['orbitos-theme']); }); + + it('also supports legacy _id user payloads as a fallback', async () => { + global.fetch = jest.fn(async (url) => { + if (url === '/api/auth/me') { + return { + ok: true, + status: 200, + json: async () => ({ + user: { _id: 'legacy-user', preferences: { music: savedMusic() } }, + }), + }; + } + if (url === '/api/files?meta=1') { + return { + ok: true, + status: 200, + json: async () => ({ files: [FILE_A, FILE_B] }), + }; + } + throw new Error(`unexpected fetch: ${url}`); + }); + renderPlayer(); + + await waitFor(() => + expect(screen.getByTestId('ids').textContent).toBe( + 'orbitos-theme,file-aaa,file-bbb', + ), + ); + }); });