Skip to content

Conversation

@nicknisi
Copy link
Member

Summary

Fixes a race condition that causes a flash of unauthenticated state during initial token fetch.

Problem

useAccessToken was reporting loading: false while fetching the initial token, causing downstream consumers to briefly show unauthenticated state before the token arrived.

This issue necessitated workarounds like checking !accessToken in loading calculations (see workos/template-convex-nextjs-authkit#5), which breaks for logged-out users.

Solution

  • Add isInitialTokenLoading state to track initial token fetch
  • Report loading: true until the initial fetch completes
  • Logged-out users continue to see loading: false as expected

Testing

Updated tests to verify the loading state is shown during initial token fetch.

Previously, useAccessToken would report loading:false during the initial token fetch,
creating a race condition where useAuth had finished loading but the token hadn't
arrived yet. This caused downstream consumers to see a brief "unauthenticated" state.

This led to workarounds like adding !accessToken to loading calculations (see
workos/template-convex-nextjs-authkit#5), which then broke for logged-out users.

This fix ensures useAccessToken correctly reports loading:true during initial fetch,
eliminating the race condition at its source. Downstream consumers can now safely
check loading states without needing the !accessToken workaround.

Changes:
- Track initial token fetch with isInitialTokenLoading state
- Report loading:true until initial fetch completes
- Update tests to expect loading state during mount
@nicknisi nicknisi force-pushed the nicknisi/flashing-fix branch from 442293c to 216032f Compare August 29, 2025 16:14
@nicknisi nicknisi requested a review from Copilot August 29, 2025 16:17

This comment was marked as outdated.

nicknisi and others added 2 commits August 29, 2025 11:22
@nicknisi nicknisi requested a review from Copilot September 2, 2025 18:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a race condition where useAccessToken incorrectly reported loading: false during initial token fetch, causing UI components to briefly flash an unauthenticated state before the token was retrieved.

  • Added isInitialTokenLoading state to track when an initial token fetch is in progress
  • Modified loading calculation to combine both initial fetch loading and token store loading states
  • Updated test cases to verify loading state is properly shown during initial token fetch

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/components/useAccessToken.ts Implemented initial token loading state tracking and combined loading logic
tests/useAccessToken.spec.tsx Updated test expectations and added new tests for loading state behavior

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@nicknisi nicknisi merged commit 82da272 into main Sep 2, 2025
4 checks passed
@nicknisi nicknisi deleted the nicknisi/flashing-fix branch September 2, 2025 22:16
@nicknisi nicknisi mentioned this pull request Sep 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants