Skip to content

Conversation

@sundaray
Copy link
Contributor

@sundaray sundaray commented Dec 28, 2025

This PR replaces 10 separate useState calls inside authkit-provider.tsx with a single discriminated union:

type AuthState =
 | {
     status: 'loading';
   }
 | { status: 'unauthenticated'; data: Omit<NoUserInfo, 'accessToken'> }
 | { status: 'authenticated'; data: Omit<UserInfo, 'accessToken'> };

This brings two benefits to the codebase:

  • Makes it impossible for anyone to unintentionally create an invalid auth state combination (which is possible now).
  • Makes it immediately clear which auth state is being handled at any point in the code.

Note: There are no changes to the public API. All existing tests pass.

@sundaray sundaray requested a review from a team as a code owner December 28, 2025 08:55
@sundaray sundaray requested a review from amygdalama December 28, 2025 08:55
@amygdalama amygdalama requested review from nicknisi and removed request for amygdalama December 29, 2025 22:18
Comment on lines -123 to -124
} finally {
setLoading(false);
Copy link
Member

Choose a reason for hiding this comment

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

If this action call fails above then without this finally the loading state stays loading forever. We probably need to keep this in some form to reset the loading state.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

const [featureFlags, setFeatureFlags] = useState<string[] | undefined>(initialAuth?.featureFlags);
const [impersonator, setImpersonator] = useState<Impersonator | undefined>(initialAuth?.impersonator);
const [loading, setLoading] = useState(!initialAuth);
const [authState, setAuthState] = useState<AuthState>(() => createAuthState(initialAuth));
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this should be a useReducer call instead? That way the auth state and loading boolean can be updated independently?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. I have replaced useState with useReducer.

@sundaray
Copy link
Contributor Author

sundaray commented Jan 6, 2026

@nicknisi I have updated the PR with the useReducer refactor.

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.

2 participants