React SDK
SDK officiel AuthSafe pour les applications React. intégration React avec hooks, composants et providers pour une authentification sécurisée OAuth 2.0 et OpenID Connect.
Overview
The authsafe-react SDK provides a full-featured authentication solution for React applications built on top of oidc-client-ts and TanStack Query. It handles all the complexity of OAuth 2.0 / OpenID Connect flows with a simple, declarative API.
Installation
npm install
npm install authsafe-reactQuick Start
Install the package using your preferred package manager:
1. Install the SDK
import { AuthProvider } from 'authsafe-react';
function App() {
return (
<AuthProvider
config={{
clientId: 'your-client-id',
redirectUri: 'http://localhost:3000/callback',
scope: ['openid', 'profile', 'email'],
env: 'production'
}}
>
<YourApp />
</AuthProvider>
);
}2. Wrap your app with AuthProvider
import { useLogin, useAuth, useLogout } from 'authsafe-react';
function MyApp() {
const { login } = useLogin();
const { user, isAuthenticated } = useAuth();
const { logout } = useLogout();
if (!isAuthenticated) {
return <button onClick={() => login()}>Login</button>;
}
return (
<div>
<h1>Welcome, {user?.name}!</h1>
<button onClick={logout}>Logout</button>
</div>
);
}Available APIs
Explore the available APIs in the React SDK:
Providers
- AuthProvider — Root authentication context provider
Hooks
- useLogin() — Hook for triggering login flows
- useLogout() — Hook for signing out users
- useAuth() — Hook for accessing authentication state
- useAuthCallback() — Hook for handling OAuth callbacks
Components
- Profile — Pre-built profile management component
Configuration Options
interface AuthConfig {
clientId: string; // Required: Your application's client ID
redirectUri: string; // Required: OAuth callback URL
scope?: Array<AuthScope>; // Optional: Requested scopes
env?: 'development' | 'production'; // Optional: Environment
authority?: string; // Optional: Custom authority URL
popupRedirectUri?: string; // Optional: Popup redirect URL
automaticSilentRenew?: boolean; // Optional: Auto-renew tokens
}