React web adapter for the @zestic/oauth-core
library, enabling consistent OAuth flows across web and mobile platforms.
This package provides React-specific implementations and components for OAuth authentication, built on top of @zestic/oauth-core
. It includes web-optimized adapters for storage (localStorage), HTTP requests (fetch API), and PKCE (Web Crypto API).
- 🔐 Secure OAuth 2.0 flows with PKCE support
- ⚛️ React hooks and components for easy integration
- 🌐 Web-optimized adapters using browser APIs
- 📱 Consistent API with mobile implementations
- 🎯 TypeScript support with full type safety
- 🧪 Framework agnostic - works with React Router, Next.js, Vite, etc.
npm install @zestic/oauth-react @zestic/oauth-core
# or
yarn add @zestic/oauth-react @zestic/oauth-core
# or
pnpm add @zestic/oauth-react @zestic/oauth-core
- Node.js 20 or higher
- React 18 or higher
import { OAuthConfig } from '@zestic/oauth-core';
const oauthConfig: OAuthConfig = {
clientId: 'your-client-id',
endpoints: {
authorization: 'https://auth.example.com/oauth/authorize',
token: 'https://auth.example.com/oauth/token',
revocation: 'https://auth.example.com/oauth/revoke',
},
redirectUri: `${window.location.origin}/auth/callback`,
scopes: ['read', 'write'],
};
import { OAuthProvider } from '@zestic/oauth-react';
function App() {
return (
<OAuthProvider config={oauthConfig}>
{/* Your app components */}
</OAuthProvider>
);
}
// React Router 6 example
import { useNavigate, useSearchParams } from 'react-router-dom';
import { OAuthCallbackPage, ReactRouterParameterExtractor } from '@zestic/oauth-react';
function AuthCallback() {
const navigate = useNavigate();
const [searchParams] = useSearchParams();
return (
<OAuthCallbackPage
config={oauthConfig}
parameterExtractor={new ReactRouterParameterExtractor(searchParams)}
onSuccess={(result) => {
console.log('Authentication successful:', result);
// Your app handles navigation
navigate('/dashboard');
}}
onError={(error) => {
console.error('Authentication failed:', error);
// Your app handles error navigation
navigate('/login?error=oauth_failed');
}}
/>
);
}
import { ReactOAuthAdapter } from '@zestic/oauth-react';
function LoginButton() {
const handleLogin = async () => {
const adapter = new ReactOAuthAdapter(oauthConfig);
const { url } = await adapter.generateAuthorizationUrl();
// Your app handles the redirect
window.location.href = url;
};
return <button onClick={handleLogin}>Login with OAuth</button>;
}
This library follows the same pattern as @zestic/oauth-expo
- it's navigation-agnostic. This means:
✅ No hard-coded navigation - Your app controls all routing
✅ Router flexibility - Works with React Router, Next.js, or any routing solution
✅ Callback-based - Uses onSuccess
/onError
callbacks instead of automatic redirects
✅ Parameter extraction - Pluggable system for different router parameter handling
import { useNavigate, useSearchParams } from 'react-router-dom';
import { ReactRouterParameterExtractor } from '@zestic/oauth-react';
// Use React Router's parameter extraction
const [searchParams] = useSearchParams();
const parameterExtractor = new ReactRouterParameterExtractor(searchParams);
import { useRouter } from 'next/router';
import { CustomParameterExtractor } from '@zestic/oauth-react';
// Use Next.js router
const router = useRouter();
const parameterExtractor = new CustomParameterExtractor(() =>
new URLSearchParams(window.location.search)
);
import { BrowserParameterExtractor } from '@zestic/oauth-react';
// Use browser's window.location.search
const parameterExtractor = new BrowserParameterExtractor();
OAuthProvider
- Context provider for OAuth configurationOAuthCallbackPage
- Handles OAuth callback flow
useOAuthCallback
- Manages OAuth callback stateuseOAuthConfig
- OAuth configuration managementuseOAuthState
- Authentication state management
ReactOAuthAdapter
- Main OAuth adapter for ReactWebStorageAdapter
- localStorage implementationWebHttpAdapter
- fetch API implementationWebPKCEAdapter
- Web Crypto API PKCE implementation
urlUtils
- URL parameter handling utilitiescryptoUtils
- Cryptographic utility functions
Examples for different React frameworks are available in the examples/
directory:
- React Router - Standard React SPA setup
- Next.js - Server-side rendering support
- Vite - Modern build tool integration
- Node.js 20+
- npm, yarn, or pnpm
# Install dependencies
npm install
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Build the package
npm run build
# Lint code
npm run lint
# Format code
npm run format
The package uses Vitest for testing with jsdom environment for browser API simulation.
# Run all tests
npm test
# Run tests with coverage
npm run test:coverage
# Run tests in watch mode
npm run test:watch
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
MIT License - see LICENSE file for details.
@zestic/oauth-core
- Core OAuth functionality@zestic/oauth-expo
- Expo/React Native adapter
For questions and support, please open an issue on GitHub.