A professional React TypeScript project with OAuth authentication, custom API client, dependency injection, and comprehensive testing.
- π OAuth 2.0 Authentication - Complete OAuth flow with GitHub integration
- π Automatic Token Refresh - Seamless token renewal with retry logic
- ποΈ Dependency Injection - Inversify.js for clean architecture
- π‘ Custom API Client - Axios-based HTTP client with interceptors
- π§ͺ Comprehensive Testing - Unit tests with Vitest and Testing Library
- π± Responsive UI - Tailwind CSS for modern design
- π§ Modern Tooling - Vite, TypeScript, ESLint
- π¦ Production Ready - Optimized build with code splitting
src/
βββ components/ # React components
β βββ Dashboard.tsx # Main dashboard component
β βββ Login.tsx # Login page component
β βββ AuthCallback.tsx # OAuth callback handler
β βββ PrivateRoute.tsx # Protected route wrapper
βββ services/ # Business logic services
β βββ AuthService.ts # Authentication service
β βββ TokenManager.ts # Token management
β βββ HttpClient.ts # HTTP client with interceptors
β βββ ApiClient.ts # Domain-specific API methods
βββ stores/ # State management
β βββ authStore.ts # Zustand auth store
βββ di/ # Dependency injection
β βββ container.ts # IoC container configuration
βββ types/ # TypeScript definitions
β βββ index.ts # Shared interfaces and types
βββ config/ # Configuration
β βββ oauth.ts # OAuth and API configuration
βββ App.tsx # Main application component
tests/
βββ components/ # Component tests
βββ services/ # Service tests
βββ mocks/ # Mock handlers for MSW
βββ setup.ts # Test configuration
- Node.js 18+
- npm or yarn
- OAuth app credentials (GitHub, Google, etc.)
- Clone the repository:
git clone <repository-url>
cd react-oauth-api-client- Install dependencies:
npm install- Configure environment variables:
cp .env.example .envEdit .env with your OAuth credentials:
VITE_OAUTH_CLIENT_ID=your_github_client_id
VITE_OAUTH_REDIRECT_URI=http://localhost:5173/auth/callback
VITE_OAUTH_SCOPE=read:user user:email
VITE_OAUTH_AUTH_URL=https://github.com/login/oauth/authorize
VITE_OAUTH_TOKEN_URL=https://github.com/login/oauth/access_token
VITE_OAUTH_USER_INFO_URL=https://api.github.com/user
VITE_API_BASE_URL=https://api.github.comStart the development server:
npm run devThe application will be available at http://localhost:5173
Build for production:
npm run buildPreview the production build:
npm run previewRun tests:
npm testRun tests with UI:
npm run test:uiGenerate coverage report:
npm run test:coverageCheck code quality:
npm run lintType checking:
npm run type-checkThe project uses Inversify.js for dependency injection, providing loose coupling and easy testing:
// Services are bound in the container
container.bind<IApiClient>(TYPES.ApiClient).to(ApiClient).inSingletonScope();
container.bind<IAuthService>(TYPES.AuthService).to(AuthService).inSingletonScope();
// And injected into components/services
@injectable()
export class AuthService implements IAuthService {
constructor(
@inject(TYPES.TokenManager) private tokenManager: ITokenManager,
@inject(TYPES.HttpClient) private httpClient: IHttpClient
) {}
}Automatic token refresh with retry logic:
- Tokens are stored in localStorage
- Automatic refresh before expiration
- Request interceptors handle token attachment
- Failed refresh redirects to login
The API client provides:
- Automatic authentication headers
- Request/response interceptors
- Error handling
- Type-safe methods
- Domain-specific endpoints
Using Zustand for simple, effective state management:
- Authentication state
- Loading states
- Error handling
- Actions for login/logout
- Go to GitHub Settings > Developer settings > OAuth Apps
- Create a new OAuth App with:
- Application name: Your app name
- Homepage URL:
http://localhost:5173 - Authorization callback URL:
http://localhost:5173/auth/callback
- Copy the Client ID and Client Secret to your
.envfile
The OAuth implementation is generic and can work with other providers by updating the configuration in src/config/oauth.ts.
The application builds to a dist/ folder and can be deployed to any static hosting service:
- Netlify: Deploy the
distfolder - Vercel: Connect your repository
- AWS S3: Upload
distcontents to S3 bucket - GitHub Pages: Use GitHub Actions to deploy
Remember to update the OAuth redirect URI for production!
- Fork the repository
- Create a feature branch:
git checkout -b feature/new-feature - Make your changes
- Add tests for new functionality
- Run tests and linting:
npm test && npm run lint - Commit your changes:
git commit -am 'Add new feature' - Push to the branch:
git push origin feature/new-feature - Submit a pull request
MIT License - see LICENSE file for details.
For questions or issues, please create an issue in the repository or contact the maintainers.