http://localhost:3000/callback)# Using npm
npm install authsafe-react
# Using yarn
yarn add authsafe-react
# Using pnpm
pnpm add authsafe-reactAuthProvider component. This is typically done in your root layout or main App component.
// app/layout.tsx or App.tsx
import { AuthProvider } from 'authsafe-react';
export default function RootLayout({ children }) {
return (
<AuthProvider
config={{
clientId: 'your-client-id-here',
redirectUri: 'http://localhost:3000/callback',
scope: ['openid', 'profile', 'email'],
env: 'production',
}}
>
{children}
</AuthProvider>
);
}// app/callback/page.tsx
'use client';
import { useOAuthCallback } from 'authsafe-react';
export default function CallbackPage() {
const { isLoading, error } = useOAuthCallback();
if (isLoading) {
return <div>Processing login...</div>;
}
if (error) {
return <div>Error: {error.message}</div>;
}
return <div>Login successful! Redirecting...</div>;
}'use client';
import { useLogin, useLogout, useAuth } from 'authsafe-react';
export default function AuthButtons() {
const { signinRedirect, isLoading: loginLoading } = useLogin();
const { logout, isLoading: logoutLoading } = useLogout();
const { isAuthenticated, user } = useAuth();
if (isAuthenticated) {
return (
<div>
<p>Welcome, {user?.name || user?.email}!</p>
<button
onClick={() => logout()}
disabled={logoutLoading}
>
{logoutLoading ? 'Logging out...' : 'Logout'}
</button>
</div>
);
}
return (
<button
onClick={() => signinRedirect()}
disabled={loginLoading}
>
{loginLoading ? 'Logging in...' : 'Login with AuthSafe'}
</button>
);
}'use client';
import { useAuth } from 'authsafe-react';
import { useRouter } from 'next/navigation';
import { useEffect } from 'react';
export default function ProtectedPage() {
const { isAuthenticated, isLoading } = useAuth();
const router = useRouter();
useEffect(() => {
if (!isLoading && !isAuthenticated) {
router.push('/');
}
}, [isAuthenticated, isLoading, router]);
if (isLoading) {
return <div>Loading...</div>;
}
if (!isAuthenticated) {
return null;
}
return (
<div>
<h1>Protected Content</h1>
<p>Only authenticated users can see this!</p>
</div>
);
}import { AuthProvider, useLogin, useAuth, useLogout } 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>
);
}
function LoginButton() {
const { signinRedirect, isLoading } = useLogin();
return (
<button onClick={() => signinRedirect()} disabled={isLoading}>
{isLoading ? 'Logging in...' : 'Login with AuthSafe'}
</button>
);
}
function UserProfile() {
const { user, isAuthenticated } = useAuth();
const { logout } = useLogout();
if (!isAuthenticated) {
return <LoginButton />;
}
return (
<div>
<h2>Welcome, {user?.name || user?.email}</h2>
<button onClick={() => logout()}>Logout</button>
</div>
);
}Learn about all available hooks, components, and utilities in the React SDK documentation.
Implement popup login, silent token renewal, and custom authentication flows.
Explore the complete API documentation for all endpoints and configuration options.
You've successfully integrated AuthSafe into your application. Your users can now securely authenticate using industry-standard OAuth 2.0 and OpenID Connect protocols.