The authorizationDocumentation 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.
code returned by onSuccess is a short-lived, single-use token. To get a usable access token you must exchange it on your backend using your Client Secret. This exchange must never happen in the browser.
Why the exchange must be server-side
The code-for-token exchange requires your Client Secret. If you performed it client-side:- Your Client Secret would be visible in browser network requests.
- Anyone who found it could impersonate your OAuth App and make requests on behalf of any user.
- GitHub would eventually revoke the secret, breaking authentication for all users.
Authorization code flow
Here is the sequence of events from click to authenticated user:- User clicks your login button.
- Your React app opens a GitHub popup and sends
client_id,redirect_uri,scope, andstate. - User authorizes on GitHub.
- GitHub redirects the popup to your
redirect_uriwithcodeandstate. - The hook verifies
stateand callsonSuccesswith thecode. - Your frontend sends
codeto your backend (e.g.,POST /api/github/callback). - Your backend calls GitHub’s token endpoint with
client_id,client_secret, andcode. - GitHub returns an
access_token. - Your backend calls
GET https://api.github.com/userwith the access token. - GitHub returns the user’s profile.
- Your backend creates or looks up the user in your database and returns a session token to the frontend.
Backend examples
Token exchange endpoint
The GitHub token exchange endpoint:| Field | Required | Description |
|---|---|---|
client_id | Yes | Your OAuth App’s Client ID |
client_secret | Yes | Your OAuth App’s Client Secret |
code | Yes | The authorization code from the OAuth callback |
redirect_uri | No | Must match the redirect URI used in the initial request, if one was specified |
Accept: application/json):
| Field | Description |
|---|---|
access_token | The GitHub access token |
scope | Scopes granted, comma-separated |
token_type | Always "bearer" |
error | Present only when the exchange fails |
error_description | Human-readable error detail |
Using the access token
Once you have the access token, use it to call GitHub API endpoints:GitHub user response fields
TheGET /user endpoint returns an object with many fields. The most commonly used ones:
| Field | Type | Description |
|---|---|---|
id | number | Unique numeric GitHub user ID |
login | string | GitHub username (e.g., "octocat") |
name | string | null | Display name (may be null) |
email | string | null | Primary email (null if not public; request user:email scope for guaranteed access) |
avatar_url | string | URL to the user’s profile picture |
html_url | string | URL to the user’s GitHub profile |
bio | string | null | User’s biography |
company | string | null | User’s company |
location | string | null | User’s location |
If
email is null, the user has not made their email public. To always receive an email address, request the user:email scope (the default) and call GET https://api.github.com/user/emails to retrieve all email addresses including private ones.Frontend: sending the code to your backend
In your React component, forward the authorization code to your backend endpoint insideonSuccess:

