fix: prevent infinite revalidation loop in auth guard

If the server consistently fails to populate currentUserId after JWT
refresh (e.g. misconfigured CLERK_SECRET_KEY), the previous effect
would loop indefinitely. A one-shot ref ensures we attempt auth
recovery at most once per mount.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Chris Parsons 2026-02-28 09:51:18 -08:00
parent d21ff5d977
commit 5b075ec1d2

View file

@ -271,10 +271,14 @@ export default function DraftRoom() {
}, [reconnectCount, revalidate]);
// Guard against Clerk JWT expiry race: if the loader ran without a userId (expired
// short-lived JWT) but the Clerk client SDK has a valid session, revalidate so the
// loader re-runs with a fresh token and returns correct userTeam/userQueue data.
// short-lived JWT) but the Clerk client SDK has a valid session, revalidate once so
// the loader re-runs with a fresh token and returns correct userTeam/userQueue data.
// The ref prevents an infinite loop if the server consistently fails to return a
// userId (e.g. misconfigured secret key) — we only attempt this recovery once.
const authRevalidatedRef = useRef(false);
useEffect(() => {
if (clerkUserId && !currentUserId) {
if (clerkUserId && !currentUserId && !authRevalidatedRef.current) {
authRevalidatedRef.current = true;
revalidate();
}
}, [clerkUserId, currentUserId, revalidate]);