Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/components/LockScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
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);
Expand All @@ -36,8 +36,7 @@

useEffect(() => {
(isLocalAdmin ? buttonRef : inputRef).current?.focus();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [isLocalAdmin]);

const name = user?.displayName || user?.username || 'User';

Expand Down Expand Up @@ -96,7 +95,7 @@

<div className="mt-8 flex flex-col items-center gap-3">
{user?.avatar ? (
<img

Check warning on line 98 in src/components/LockScreen.js

View workflow job for this annotation

GitHub Actions / Lint

Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` or a custom image loader to automatically optimize images. This may incur additional usage or cost from your provider. See: https://nextjs.org/docs/messages/no-img-element
src={user.avatar}
alt=""
className="h-24 w-24 rounded-full object-cover ring-2 ring-white/60"
Expand Down
2 changes: 1 addition & 1 deletion src/context/MusicContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions tests/LockScreen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(<LockScreen user={null} onUnlock={onUnlock} />);

// Hydrates asynchronously from GET /api/auth/me, which returns `id` (no `_id`).
rerender(
<LockScreen
user={{
id: '000000000000000000000000',
username: 'local-admin',
displayName: 'Local Admin',
}}
onUnlock={onUnlock}
/>,
);

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', () => {
Expand Down
37 changes: 33 additions & 4 deletions tests/MusicPersistence.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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() } },
}),
};
}
Expand Down Expand Up @@ -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',
),
);
});
});
Loading