Skip to content

refactor(auth): Migrate auth to Tanstack Query#662

Merged
CodeWithCJ merged 1 commit intoCodeWithCJ:mainfrom
Sim-sat:migrate-auth
Feb 15, 2026
Merged

refactor(auth): Migrate auth to Tanstack Query#662
CodeWithCJ merged 1 commit intoCodeWithCJ:mainfrom
Sim-sat:migrate-auth

Conversation

@Sim-sat
Copy link
Contributor

@Sim-sat Sim-sat commented Feb 15, 2026

Description

This commit continues the migration with the auth pages. I didn't touch the passkey and mfa functions since they already use the better auth client.

Part of #657

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Sim-sat, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request continues the ongoing migration of the frontend application to use Tanstack Query, specifically focusing on the authentication pages. The primary goal is to modernize the data fetching and state management for various authentication flows, leading to a more robust and maintainable codebase. This refactoring centralizes authentication logic into reusable hooks and separates concerns, improving the overall architecture of the authentication module.

Highlights

  • Tanstack Query Migration: Migrated core authentication logic, including login, registration, magic link, password reset, and OIDC flows, to utilize Tanstack Query for improved state management and data fetching.
  • New Authentication Hooks: Introduced a new useAuth.ts file containing custom Tanstack Query hooks (useLoginUserMutation, useRegisterUserMutation, useRequestMagicLinkMutation, useVerifyMagicLinkMutation, useAuthSettings, useOidcProvidersQuery, useInitiateOidcLoginMutation, useRequestPasswordResetMutation, useResetPasswordMutation, mfaFactorsOptions) to encapsulate authentication-related data operations.
  • Refactored Auth Pages: Updated Auth.tsx, ForgotPassword.tsx, and ResetPassword.tsx to consume the newly created Tanstack Query hooks, removing direct API service calls and local state management for authentication settings and OIDC providers.
  • Centralized Query Keys: Created a dedicated auth.ts file under src/api/keys to centralize and manage Tanstack Query keys for authentication, enhancing cache invalidation and query organization.
  • Toast Notification Handling: Removed explicit toast calls from mutation handlers in the UI components, as success and error messages are now configured directly within the meta option of the Tanstack Query useMutation hooks, streamlining error and success feedback.
  • Component Extraction: Extracted the MagicLinkRequestDialog component from Auth.tsx into its own dedicated file (MagicLinkRequestDialog.tsx) for better modularity and reusability.
Changelog
  • SparkyFitnessFrontend/src/api/keys/auth.ts
    • Added a new file to define and export Tanstack Query keys for authentication-related data.
  • SparkyFitnessFrontend/src/hooks/Auth/useAuth.ts
    • Created a new file to house custom Tanstack Query hooks for various authentication operations, including user login, registration, magic link requests, OIDC provider fetching, password reset, and MFA factor retrieval.
    • Implemented useTranslation within mutation hooks to provide localized success and error messages.
  • SparkyFitnessFrontend/src/pages/Auth/Auth.tsx
    • Removed direct imports of authentication service functions and related types.
    • Integrated new Tanstack Query hooks for login, registration, magic link, OIDC, and authentication settings.
    • Replaced local state management for loginSettings and oidcProviders with data from useAuthSettings and useOidcProvidersQuery.
    • Updated the useEffect hook to use data from Tanstack Query hooks for auto-redirect logic.
    • Refactored handleSignUp, handleRequestMagicLink, and handleSignIn functions to use the mutateAsync methods provided by the new Tanstack Query mutation hooks.
    • Modified MFA factor fetching to use queryClient.fetchQuery with mfaFactorsOptions.
    • Removed the inline MagicLinkRequestDialog component definition, now importing it from its dedicated file.
    • Adjusted conditional rendering to safely access properties of loginSettings and oidcProviders using optional chaining.
  • SparkyFitnessFrontend/src/pages/Auth/ForgotPassword.tsx
    • Removed direct import of requestPasswordReset service function and toast utility.
    • Integrated useRequestPasswordResetMutation hook to handle password reset requests.
    • Updated the handleSubmit function to use the mutateAsync method from the new hook, relying on the hook's meta option for toast notifications.
  • SparkyFitnessFrontend/src/pages/Auth/MagicLinkRequestDialog.tsx
    • Added a new file to define and export the MagicLinkRequestDialog component, previously embedded in Auth.tsx.
  • SparkyFitnessFrontend/src/pages/Auth/ResetPassword.tsx
    • Removed direct import of resetPassword service function and toast utility.
    • Integrated useResetPasswordMutation hook to handle password reset confirmation.
    • Updated the handleSubmit function to use the mutateAsync method from the new hook, relying on the hook's meta option for toast notifications.
  • SparkyFitnessFrontend/src/services/authService.ts
    • Added a new getMfaFactors function to fetch multi-factor authentication factors for a given email.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors authentication-related logic by introducing a new authKeys file for React Query keys and a useAuth.ts hook file that encapsulates various authentication API calls (login, register, magic link, OIDC, password reset, MFA factors) into custom React Query hooks. The Auth.tsx, ForgotPassword.tsx, and ResetPassword.tsx components were updated to utilize these new hooks, replacing direct authService calls and local state management for data fetching. This also involved removing explicit toast notifications and some error logging from these components, as success/error messages are now handled within the meta option of the React Query mutations. Additionally, the MagicLinkRequestDialog component was extracted into its own file. Review comments highlight the need to reintroduce error logging in ForgotPassword.tsx and ResetPassword.tsx for consistency and debugging, and to implement error handling for the useAuthSettings query in Auth.tsx to provide user feedback when login settings fail to load.

useState(false);

const queryClient = useQueryClient();
const { data: loginSettings } = useAuthSettings();
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The previous implementation handled errors when fetching login settings by displaying a warning to the user. This was removed in the refactoring. If the useAuthSettings query fails (e.g., due to a network error), the user currently receives no feedback. It would be beneficial to handle the error state from the query to improve the user experience, for instance by showing an error message in the UI.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not true is handled by the client directly.

Comment on lines 37 to 39
} catch (err: any) {
error(
loggingLevel,
'ForgotPassword: Password reset request failed:',
err
);
setMessage(err.message || 'An unexpected error occurred.');
toast({
title: 'Error',
description: err.message || 'Failed to send password reset email.',
variant: 'destructive',
});
} finally {
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The error logging was removed from this catch block. For consistency with other error handling in the application (like in Auth.tsx) and to aid in debugging, it's recommended to log the error here. You will also need to re-import error from @/utils/logging.

} catch (err: any) {
      error(
        loggingLevel,
        'ForgotPassword: Password reset request failed:',
        err
      );
      setMessage(err.message || 'An unexpected error occurred.');
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The hooks have logging defined in the query client

Comment on lines 99 to 101
} catch (err: any) {
error(loggingLevel, 'ResetPassword: Password reset failed:', err);
setMessage(err.message || 'An unexpected error occurred.');
toast({
title: 'Error',
description: err.message || 'Failed to reset password.',
variant: 'destructive',
});
} finally {
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Similar to ForgotPassword.tsx, the error logging has been removed from this catch block. It's best to log this error for debugging purposes and to maintain consistency across the codebase. Please also re-import error from @/utils/logging.

} catch (err: any) {
      error(loggingLevel, 'ResetPassword: Password reset failed:', err);
      setMessage(err.message || 'An unexpected error occurred.');
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The hooks have logging defined in the query client

Migration of the next part of the app to Tanstack query following the
same procedure.
@CodeWithCJ
Copy link
Owner

@Sim-sat there were some custom logic from global settings and user settings and it dynamically use to show OIDC, MFA or email one time code , backup code etc. Are they all tested?

@CodeWithCJ CodeWithCJ merged commit d6ebedc into CodeWithCJ:main Feb 15, 2026
6 checks passed
@Sim-sat Sim-sat deleted the migrate-auth branch February 15, 2026 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants