Skip to content

fix(auth): Revalidate user data on login to prevent stale state#86

Open
ankush210103 wants to merge 1 commit into
frappe:mainfrom
ankush210103:fix-auth-revalidate
Open

fix(auth): Revalidate user data on login to prevent stale state#86
ankush210103 wants to merge 1 commit into
frappe:mainfrom
ankush210103:fix-auth-revalidate

Conversation

@ankush210103
Copy link
Copy Markdown

This PR fixes a bug in the useFrappeAuth hook where the currentUser state would remain stale (or undefined) after a successful user login.

What was the problem?

After a user successfully logged in using the login function, the underlying SWR cache for the user session was not being invalidated. This meant that even though the authentication was successful on the backend, the hook continued to return the old, pre-login state, forcing developers to manually refresh the page to see the updated user status.

How does this fix it?

This PR resolves the issue by calling updateCurrentUser() (the mutate function returned by useSWR) immediately after the login promise resolves successfully.

This action tells SWR to revalidate the data for the user session key, triggering a fresh API call to frappe.auth.get_logged_user. The hook now correctly updates with the new user's data, ensuring the UI reflects the logged-in state instantly.

I have also added updateCurrentUser to the useCallback dependency array for the login function to adhere to React hooks best practices.

How to Test

  1. Set up the example application and connect it to a local Frappe instance.
  2. Create a simple login form in App.tsx using the useFrappeAuth hook.
  3. Display the currentUser value on the screen (e.g., currentUser ?? 'Not Logged In').
  4. Fill in the credentials and click the login button.
  5. Observe that the displayed user name immediately updates upon successful login without a page refresh.**

@vercel
Copy link
Copy Markdown

vercel Bot commented Sep 21, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
frappe-react-sdk Error Error Sep 21, 2025 11:24am

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.

1 participant