Fix bug with saved loginKey

This commit is contained in:
Jason Laster 2025-03-20 08:56:17 -07:00
parent a5e53a023b
commit bb3d2b96a9
3 changed files with 56 additions and 3 deletions

View File

@ -1,6 +1,8 @@
import { atom } from 'nanostores';
import { getNutIsAdmin, getUsername } from '~/lib/replay/Problems';
import { userStore } from './auth';
import { useStore } from '@nanostores/react';
import { useEffect } from 'react';
// Store for admin status
export const isAdminStore = atom<boolean>(false);
@ -21,6 +23,20 @@ export function updateUsername(username: string | undefined) {
}
}
export function useAdminStatus() {
const isAdmin = useStore(isAdminStore);
useEffect(() => {
if (typeof window !== 'undefined') {
refreshAdminStatus();
}
}, []);
return {
isAdmin,
};
}
// Initialize the user stores
export async function initializeUserStores() {
try {
@ -39,7 +55,7 @@ export async function initializeUserStores() {
// Subscribe to user changes to update admin status
return userStore.subscribe(async (user) => {
if (user) {
// When user changes, recalculate admin status
// When user changes, reverify admin status
const isAdmin = await getNutIsAdmin();
isAdminStore.set(isAdmin);
} else {
@ -52,3 +68,23 @@ export async function initializeUserStores() {
return undefined;
}
}
/*
* Function to trigger a re-verification of admin status
* This can be called manually if needed
*/
async function refreshAdminStatus(): Promise<boolean> {
if (typeof window === 'undefined') {
return false;
}
try {
const isAdmin = await getNutIsAdmin();
isAdminStore.set(isAdmin);
return isAdmin;
} catch (error) {
console.error('Failed to refresh admin status', error);
return isAdminStore.get();
}
}

View File

@ -14,7 +14,7 @@ import {
deleteProblem as backendDeleteProblem,
BoltProblemStatus,
} from '~/lib/replay/Problems';
import { isAdminStore, usernameStore } from '~/lib/stores/user';
import { useAdminStatus, usernameStore } from '~/lib/stores/user';
import type { BoltProblem, BoltProblemComment } from '~/lib/replay/Problems';
function Comments({ comments }: { comments: BoltProblemComment[] }) {
@ -229,7 +229,7 @@ const Nothing = () => null;
function ViewProblemPage() {
const params = useParams();
const problemId = params.id;
const isAdmin = useStore(isAdminStore);
const { isAdmin } = useAdminStatus();
if (typeof problemId !== 'string') {
throw new Error('Problem ID is required');

View File

@ -94,3 +94,20 @@ test('Should be able to update a problem', async ({ page }) => {
await page.getByRole('button', { name: 'Set Status' }).click();
await page.locator('span').filter({ hasText: 'Pending' }).click();
});
test('Confirm that isAdmin is saved correctly', async ({ page }) => {
await page.goto('/problems?showAll=true');
await page.getByRole('combobox').selectOption('all');
await page.getByRole('link', { name: '[test] playwright' }).first().click();
if (await isSupabaseEnabled(page)) {
expect(true).toBe(true);
return;
}
await setLoginKey(page);
await expect(await page.getByRole('button', { name: 'Set Status' })).toBeVisible();
await page.reload();
await expect(await page.getByRole('button', { name: 'Set Status' })).toBeVisible();
});