Cross-site request forgery (CSRF) in the OAuth context means an attacker tricks your app into processing an authorization code that was not initiated by your user. TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/MomenSherif/react-oauth/llms.txt
Use this file to discover all available pages before exploring further.
state parameter is the standard defense: your app sends a value to GitHub, and GitHub echoes it back. If the values don’t match, the response is rejected.
Automatic state generation
By default,useGitHubLogin generates a random state string on every hook render. You do not need to configure anything:
Math.random().toString(36) substrings, producing a string like "k7x2m9pq3j4nr8t". A new value is produced each time the hook renders (unless you provide your own state).
How state is verified
The hook verifies the state automatically before callingonSuccess:
- When
initiateGitHubLoginis called, the currentstatevalue is included in the GitHub authorization URL as thestatequery parameter. - After the user authorizes, GitHub redirects the popup to your
redirectUriwithcodeandstateappended. - The hook extracts both values from the popup URL and compares
response.stateagainst thestatethat was sent. - If they match (or if GitHub did not return a
state),onSuccessis called. - If they do not match, the hook calls
onErrorwith anOA003error and does not callonSuccess.
Custom state parameter
If you need to embed application-specific data in the state (e.g., a redirect path or session ID), pass your own value:Verifying state in onSuccess
The hook performs automatic state verification. However, if you use a custom state and want to extract data from it inonSuccess, you can safely access response.state — the hook guarantees it already matched:
What happens on a state mismatch
When thestate in GitHub’s callback does not match the value the hook sent, the hook:
- Calls
onErrorwith anOAuthErrorwhereerror.code === OAuthErrorCode.STATE_MISMATCH(OA003). - Does not call
onSuccess. - Closes the popup.
- Resets
isLoadingtofalse.
Security best practices
- Use the auto-generated state unless you have a specific reason to provide your own. It is random and unique per render.
- Include a random component in any custom state value so it cannot be guessed or reused by an attacker.
- Treat
OA003as a security event. Log it with enough context to investigate, but show only a generic message to the user. - Do not store sensitive data in the state. The state parameter travels through GitHub’s servers and the browser URL bar. It is not secret — it is only unpredictable.

