fix(auth): Revalidate user data on login to prevent stale state#86
Open
ankush210103 wants to merge 1 commit into
Open
fix(auth): Revalidate user data on login to prevent stale state#86ankush210103 wants to merge 1 commit into
ankush210103 wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a bug in the
useFrappeAuthhook where thecurrentUserstate would remain stale (orundefined) after a successful user login.What was the problem?
After a user successfully logged in using the
loginfunction, 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()(themutatefunction returned byuseSWR) 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
updateCurrentUserto theuseCallbackdependency array for theloginfunction to adhere to React hooks best practices.How to Test
App.tsxusing theuseFrappeAuthhook.currentUservalue on the screen (e.g.,currentUser ?? 'Not Logged In').