UNPKG

@j2blasco/ts-auth

Version:

TypeScript authentication abstraction library that eliminates vendor lock-in and provides mock-free testing for both frontend and backend authentication systems

225 lines (161 loc) โ€ข 6.91 kB
# @j2blasco/ts-auth A TypeScript authentication abstraction library that eliminates vendor lock-in and provides mock-free testing for both frontend and backend authentication systems. ## ๐ŸŽฏ Why This Library? **Problem**: Authentication providers (Firebase, Auth0, AWS Cognito) lock you into their specific APIs, making switching providers painful and testing complex. **Solution**: A simple abstraction layer with complete testing implementations that work with any authentication provider. ## ๐Ÿš€ Key Features - **๐Ÿ”“ Vendor Independence**: Switch between Firebase, Auth0, AWS Cognito, or any provider without changing business logic - **๐ŸŽญ No More Mocks**: Real testing implementations instead of fragile mocks - **๐Ÿ”„ Frontend + Backend**: Separate interfaces for client and server concerns - **โœ… Contract Testing**: Comprehensive test suites ensure implementations work correctly - **๐Ÿ“ฆ TypeScript First**: Full type safety with Result types for error handling - **๐Ÿงฉ Minimal API**: Simple interfaces focusing on common authentication operations ## ๐Ÿ“ฆ Installation ```bash npm install @j2blasco/ts-auth ``` ## ๐Ÿ—๏ธ Architecture The library provides separate interfaces for frontend and backend concerns: ### Frontend Components - **`IAuthFrontend`**: Client-side operations (login, signup, password reset) - **`AuthFrontendTesting`**: Complete frontend testing implementation - **`testAuthFrontend`**: Test suite for validating frontend implementations ### Backend Components - **`IAuthBackend`**: Server-side operations (token validation, user management) - **`AuthBackendTesting`**: Complete backend testing implementation - **`testAuthBackend`**: Test suite for validating backend implementations ## ๐Ÿ”ง Quick Start ### Frontend Usage ```typescript import { IAuthFrontend, AuthFrontendTesting } from '@j2blasco/ts-auth'; // In your tests - no mocks needed! const auth = new AuthFrontendTesting(); auth.addTestUser('test@example.com', 'password123'); await auth.signInWithEmailAndPassword({ email: 'test@example.com', password: 'password123', persistent: true }); // In production - implement the interface for your provider class FirebaseAuthFrontend implements IAuthFrontend { // Implement all methods... } ``` ### Backend Usage ```typescript import { IAuthBackend, AuthBackendTesting } from '@j2blasco/ts-auth'; // In your tests const backendAuth = new AuthBackendTesting(); const result = await backendAuth.signUpWithEmailPassword({ email: 'user@example.com', password: 'password123' }); const uid = result.unwrap().uid; // In production - implement for your provider class FirebaseAuthBackend implements IAuthBackend { // Implement all methods... } ``` ### Testing Your Implementations ```typescript import { testAuthFrontend, testAuthBackend } from '@j2blasco/ts-auth'; describe('My Auth Implementation', () => { // Frontend tests - comprehensive suite testAuthFrontend(() => new MyFirebaseAuthFrontend()); // Backend tests - comprehensive suite testAuthBackend(new MyFirebaseAuthBackend()); }); ``` ## ๐Ÿ” Repository Structure ``` src/ โ”œโ”€โ”€ frontend/ โ”‚ โ”œโ”€โ”€ core/ โ”‚ โ”‚ โ”œโ”€โ”€ auth-frontend.interface.ts # Frontend interface definition โ”‚ โ”‚ โ””โ”€โ”€ auth-frontend.generic.test.ts # Generic test suite โ”‚ โ””โ”€โ”€ providers/ โ”‚ โ””โ”€โ”€ testing/ โ”‚ โ”œโ”€โ”€ auth-frontend.testing.ts # Testing implementation โ”‚ โ””โ”€โ”€ auth-frontend.testing.test.ts # Implementation tests โ”œโ”€โ”€ backend/ โ”‚ โ”œโ”€โ”€ core/ โ”‚ โ”‚ โ”œโ”€โ”€ auth-backend.interface.ts # Backend interface definition โ”‚ โ”‚ โ””โ”€โ”€ auth-backend.generic.test.ts # Generic test suite โ”‚ โ””โ”€โ”€ providers/ โ”‚ โ””โ”€โ”€ testing/ โ”‚ โ”œโ”€โ”€ auth-backend.testing.ts # Testing implementation โ”‚ โ””โ”€โ”€ auth-backend.testing.test.ts # Implementation tests โ””โ”€โ”€ index.ts # Public exports ``` ## ๐Ÿ“‹ Interface Overview ### Frontend Interface (`IAuthFrontend`) User-facing authentication operations: - `authState$` - Observable authentication state - `signInWithEmailAndPassword()` - User login - `signUp()` - User registration - `signOut()` - User logout - `getIdToken()` - Get current user token - `isEmailAvailable()` - Check email availability - `changeEmail()` - Update user email - `deleteAccount()` - Delete user account - `triggerResetPasswordFlow()` - Initiate password reset - `requestChangePassword()` - Complete password change ### Backend Interface (`IAuthBackend`) Server-side authentication operations: - `onUserCreated$` / `onUserDeleted$` - User lifecycle events - `getUidFromIdToken()` - Validate and extract UID from token - `signInWithEmailAndPassword()` - Administrative signin - `signInWithRefreshToken()` - Token refresh - `signUpWithEmailPassword()` - Administrative user creation - `changeEmail()` - Administrative email change - `changePassword()` - Administrative password change - `deleteUser()` - Administrative user deletion - `getUidByEmail()` - Lookup user by email ## ๐Ÿงช Testing Philosophy This library follows **Test-Driven Development** principles: 1. **Interface-First**: Define the contract before implementation 2. **Real Objects Over Mocks**: Use complete implementations instead of mocks 3. **Contract Testing**: Generic test suites validate any implementation 4. **Fail Fast**: Comprehensive error handling with Result types ## ๐Ÿ”ง Development ### Prerequisites - Node.js 18+ - npm ### Setup ```bash git clone https://github.com/j2blasco/ts-auth.git cd ts-auth npm install ``` ### Running Tests ```bash # Run all tests npm test # Run frontend tests only npm test -- src/frontend # Run backend tests only npm test -- src/backend ``` ### Building ```bash npm run build ``` ## ๐Ÿค Contributing 1. Fork the repository 2. Create a feature branch: `git checkout -b feature/your-feature` 3. Write tests first (TDD approach) 4. Implement your feature 5. Ensure all tests pass: `npm test` 6. Submit a pull request ## ๐Ÿ› ๏ธ Example Implementations Want to see real implementations? Check out these examples: - Firebase Implementation *(coming soon)* - Auth0 Implementation *(coming soon)* - AWS Cognito Implementation *(coming soon)* ## ๐Ÿ“„ License MIT License - see [LICENSE](LICENSE) file for details. ## ๐Ÿ™‹ Support - **Issues**: [GitHub Issues](https://github.com/j2blasco/ts-auth/issues) - **Discussions**: [GitHub Discussions](https://github.com/j2blasco/ts-auth/discussions) --- **Built with โค๏ธ for developer freedom and testing sanity.**