React single-page app
Add sign-in to a React browser app as a public client, using react-oidc-context and the Authorization Code flow with PKCE.
This guide adds OIDC sign-in to a React app with no backend of its own, using react-oidc-context (built on oidc-client-ts). A browser app is a public client: it has a client ID but no secret, and relies on PKCE.
Register the application as a public / single-page OIDC client and add your redirect URI first; see Before you start. For the underlying protocol, see OpenID Connect (Authorization Code + PKCE).
1. Install
npm install react-oidc-context oidc-client-ts2. Configure the provider
Wrap your app in AuthProvider. The authority is your instance's login domain; the library discovers the endpoints and uses Authorization Code with PKCE by default.
// main.jsx
import { createRoot } from 'react-dom/client';
import { AuthProvider } from 'react-oidc-context';
import App from './App';
const oidcConfig = {
authority: 'https://<your-domain>',
client_id: '<your-client-id>',
redirect_uri: 'https://app.example.com/callback',
post_logout_redirect_uri: 'https://app.example.com/',
scope: 'openid profile email',
// Clean the code/state params out of the URL after the redirect back.
onSigninCallback: () =>
window.history.replaceState({}, document.title, window.location.pathname),
};
createRoot(document.getElementById('root')).render(
<AuthProvider {...oidcConfig}>
<App />
</AuthProvider>
);Do not put a client secret in browser code. A public client has none.
3. Sign in and read the user
Use the useAuth hook to trigger sign-in and read the result.
// App.jsx
import { useAuth } from 'react-oidc-context';
export default function App() {
const auth = useAuth();
if (auth.isLoading) return <p>Loading...</p>;
if (auth.error) return <p>Sign-in error: {auth.error.message}</p>;
if (auth.isAuthenticated) {
return (
<div>
<p>Signed in as {auth.user?.profile.email}</p>
<button onClick={() => auth.signoutRedirect()}>Log out</button>
</div>
);
}
return <button onClick={() => auth.signinRedirect()}>Log in</button>;
}auth.user.profile holds the ID-token claims (such as sub, name, email). auth.user.access_token is the token you send to your own APIs as Authorization: Bearer <token>.
4. Handle the redirect
react-oidc-context processes the callback automatically when the app loads at the redirect_uri, as long as the AuthProvider wraps the component tree. Point your redirect URI at a route your app serves (for example /callback), and make sure that route renders the same provider-wrapped app.
Troubleshooting
- The redirect loops or the URL keeps the
codeparameter. Add theonSigninCallbackhandler shown above so the one-time parameters are cleared after processing. redirect_urimismatch. Theredirect_urimust match a value registered on the application exactly, including the path. Register the SPA callback URL you use.- Tokens vanish on refresh. By default tokens are held in memory and are lost on a full reload. Configure a user store (for example
WebStorageStateStorewithlocalStorage) if you need them to persist, and weigh the security trade-off of storing tokens in the browser. invalid_client. The application must be registered as a public (PKCE) client. A confidential client expects a secret the browser cannot provide.
Next
- Node / Express backend if you add a server component.
- APIs to protect your own API with the access token.