|
1 | | -# auth_repository |
| 1 | +<div align="center"> |
| 2 | + <img src="https://avatars.githubusercontent.com/u/202675624?s=400&u=dc72a2b53e8158956a3b672f8e52e39394b6b610&v=4" alt="Flutter News App Toolkit Logo" width="220"> |
| 3 | + <h1>Auth Repository</h1> |
| 4 | + <p><strong>A repository that provides an abstraction layer over authentication operations for the Flutter News App Toolkit.</strong></p> |
| 5 | +</div> |
2 | 6 |
|
3 | | - |
4 | | -[](https://pub.dev/packages/very_good_analysis) |
5 | | -[](https://polyformproject.org/licenses/free-trial/1.0.0) |
| 7 | +<p align="center"> |
| 8 | + <img src="https://img.shields.io/badge/coverage-100%25-green?style=for-the-badge" alt="coverage"> |
| 9 | + <a href="https://flutter-news-app-full-source-code.github.io/docs/"><img src="https://img.shields.io/badge/LIVE_DOCS-VIEW-slategray?style=for-the-badge" alt="Live Docs: View"></a> |
| 10 | + <a href="https://github.com/flutter-news-app-full-source-code"><img src="https://img.shields.io/badge/MAIN_PROJECT-BROWSE-purple?style=for-the-badge" alt="Main Project: Browse"></a> |
| 11 | +</p> |
6 | 12 |
|
7 | | -A repository package that provides an abstraction layer over authentication operations. It wraps an `AuthClient` implementation, offering a clean interface for authentication flows, ensuring standardized exception propagation, and handling authentication token persistence using `KvStorageService`. |
| 13 | +This `auth_repository` package contains the `AuthRepository` class, which acts as an abstraction layer over an `AuthClient` implementation (from the `auth_client` package) within the [**Flutter News App Full Source Code Toolkit**](https://github.com/flutter-news-app-full-source-code). Its primary purpose is to provide a clean, business-focused interface for authentication flows, ensuring standardized exception propagation, and handling authentication token persistence using `KvStorageService`. This repository effectively decouples the application's core logic from the specifics of authentication mechanisms and token storage. |
8 | 14 |
|
9 | | -## Getting Started |
| 15 | +## ⭐ Feature Showcase: Business-Focused Authentication Management |
10 | 16 |
|
11 | | -Add the package to your `pubspec.yaml`: |
| 17 | +This package offers a comprehensive set of features for managing authentication operations. |
12 | 18 |
|
13 | | -```yaml |
14 | | -dependencies: |
15 | | - auth-repository: |
16 | | - git: |
17 | | - url: https://github.com/flutter-news-app-full-source-code/auth-repository.git |
| 19 | +<details> |
| 20 | +<summary><strong>🧱 Core Functionality</strong></summary> |
18 | 21 |
|
19 | | -## Features |
| 22 | +### 🚀 `AuthRepository` Class |
| 23 | +- **`AuthRepository`:** Provides a clean, business-focused interface for authentication-related tasks, abstracting the underlying `AuthClient` implementation. |
| 24 | +- **Complete Authentication Lifecycle:** Offers methods for a full authentication lifecycle, including `authStateChanges` (stream of user authentication state), `getCurrentUser` (retrieves current user), `requestSignInCode` (initiates email+code sign-in), `verifySignInCode` (verifies code, saves token, returns user), `signInAnonymously` (signs in anonymously, saves token, returns user), and `signOut` (signs out user, clears token). |
20 | 25 |
|
21 | | -- Abstracts authentication logic from the UI/business logic layers. |
22 | | -- Provides methods for a complete authentication lifecycle: |
23 | | - - `authStateChanges`: Stream of user authentication state. |
24 | | - - `getCurrentUser`: Retrieves the current authenticated user. |
25 | | - - `requestSignInCode`: Initiates email+code sign-in. |
26 | | - - `verifySignInCode`: Verifies the code, saves the auth token, and returns the user. |
27 | | - - `signInAnonymously`: Signs in anonymously, saves the auth token, and returns the user. |
28 | | - - `signOut`: Signs out the user and clears the auth token. |
29 | | -- Manages authentication token persistence internally using `KvStorageService`. |
30 | | - - Exposes `saveAuthToken(String token)`, `getAuthToken()`, and `clearAuthToken()` for direct token manipulation if needed, but these are typically handled by the main auth flow methods. |
31 | | -- Propagates standardized `HttpException`s from the underlying client and `StorageException`s from the storage service. |
| 26 | +### 🌐 Token Persistence Management |
| 27 | +- **`KvStorageService` Integration:** Manages authentication token persistence internally using an injected `KvStorageService`. |
| 28 | +- **Direct Token Access:** Exposes `saveAuthToken(String token)`, `getAuthToken()`, and `clearAuthToken()` for direct token manipulation, though these are typically handled by the main authentication flow methods. |
32 | 29 |
|
33 | | -## Usage |
| 30 | +### 🛡️ Standardized Error Handling |
| 31 | +- **Exception Propagation:** Propagates standardized `HttpException`s (from `core`) from the underlying client and `StorageException`s (from `kv_storage_service`), ensuring consistent and predictable error management across the application layers. |
34 | 32 |
|
35 | | -Instantiate `AuthRepository` by providing implementations of `AuthClient` and `KvStorageService`: |
| 33 | +### 💉 Dependency Injection Ready |
| 34 | +- **`AuthClient` & `KvStorageService` Dependencies:** Requires instances of `AuthClient` (from `auth_client`) and `KvStorageService` (from `kv_storage_service`) via its constructor, promoting loose coupling and testability. |
36 | 35 |
|
37 | | -```dart |
38 | | -import 'package:auth_client/auth_client.dart'; |
39 | | -import 'package:auth_repository/auth_repository.dart'; |
40 | | -import 'package:kv_storage_service/kv_storage_service.dart'; |
| 36 | +> **💡 Your Advantage:** This package provides a business-focused abstraction for authentication operations, simplifying the integration of user authentication into your application logic. It ensures consistent error handling, manages token persistence, and decouples your core application from specific authentication and storage implementations, enhancing maintainability and flexibility. |
41 | 37 |
|
42 | | -// Assume ConcreteAuthClient is an implementation of AuthClient |
43 | | -final authClient = ConcreteAuthClient(...); |
44 | | -
|
45 | | -// Assume ConcreteKVStorageService is an implementation of KvStorageService |
46 | | -final storageService = ConcreteKVStorageService(...); |
47 | | -
|
48 | | -final authRepository = AuthRepository( |
49 | | - authClient: authClient, |
50 | | - storageService: storageService, |
51 | | -); |
52 | | -
|
53 | | -// Example usage: |
54 | | -authRepository.authStateChanges.listen((user) { |
55 | | - // Handle auth state changes |
56 | | -}); |
57 | | -
|
58 | | -try { |
59 | | - await authRepository.requestSignInCode('[email protected]'); |
60 | | - // Handle success |
61 | | -} on InvalidInputException catch (e) { |
62 | | - // Handle invalid email |
63 | | -} catch (e) { |
64 | | - // Handle other errors |
65 | | -} |
66 | | -
|
67 | | -// Example usage: |
68 | | -try { |
69 | | - final user = await authRepository.verifySignInCode('[email protected]', '123456'); |
70 | | - // User is signed in, token is saved automatically. |
71 | | - print('User signed in: ${user.id}'); |
72 | | -} on AuthenticationException catch (e) { |
73 | | - // Handle invalid code |
74 | | -} on StorageException catch (e) { |
75 | | - // Handle failure to save token |
76 | | -} catch (e) { |
77 | | - // Handle other errors |
78 | | -} |
79 | | -
|
80 | | -// Example of anonymous sign-in: |
81 | | -try { |
82 | | - final anonUser = await authRepository.signInAnonymously(); |
83 | | - // User is signed in anonymously, token is saved automatically. |
84 | | - print('Anonymous user signed in: ${anonUser.id}'); |
85 | | -} catch (e) { |
86 | | - // Handle errors |
87 | | -} |
88 | | -
|
89 | | -// Example of sign-out: |
90 | | -try { |
91 | | - await authRepository.signOut(); |
92 | | - // User is signed out, token is cleared automatically. |
93 | | - print('User signed out.'); |
94 | | -} catch (e) { |
95 | | - // Handle errors |
96 | | -} |
97 | | -
|
98 | | -// Direct token access (e.g., for HTTP client interceptors): |
99 | | -Future<String?> getTokenForHttpClient() async { |
100 | | - final token = await authRepository.getAuthToken(); |
101 | | - print('Retrieved token for HTTP client: $token'); |
102 | | - return token; |
103 | | -} |
104 | | -``` |
| 38 | +</details> |
105 | 39 |
|
106 | 40 | ## 🔑 Licensing |
107 | 41 |
|
108 | | -This package is source-available and licensed under the [PolyForm Free Trial 1.0.0](LICENSE). Please review the terms before use. |
109 | | - |
110 | | -For commercial licensing options that grant the right to build and distribute unlimited applications, please visit the main [**Flutter News App - Full Source Code Toolkit**](https://github.com/flutter-news-app-full-source-code) organization. |
| 42 | +This `auth_repository` package is an integral part of the [**Flutter News App Full Source Code Toolkit**](https://github.com/flutter-news-app-full-source-code). For comprehensive details regarding licensing, including trial and commercial options for the entire toolkit, please refer to the main toolkit organization page. |
0 commit comments