|
1 | | -# auth_inmemory |
| 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 In-Memory</h1> |
| 4 | + <p><strong>An in-memory implementation of the `AuthClient` interface 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-13%25-red?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 | | -An in-memory implementation of the `AuthClient` interface. This package provides a mock authentication client that operates entirely on in-memory data, making it suitable for demonstration purposes, local development, and testing without requiring a live backend. |
| 13 | +This `auth_inmemory` package provides an in-memory implementation of the `AuthClient` interface within the [**Flutter News App Full Source Code Toolkit**](https://github.com/flutter-news-app-full-source-code). It offers a mock authentication client that operates entirely on in-memory data, making it suitable for demonstration purposes, local development, and testing without requiring a live backend. This package simulates various authentication flows, ensuring consistent behavior and robust error handling based on the `auth_client` contract. |
8 | 14 |
|
9 | | -### Getting Started |
| 15 | +## ⭐ Feature Showcase: Simplified Authentication Testing & Development |
10 | 16 |
|
11 | | -Add the following to your `pubspec.yaml` dependencies: |
| 17 | +This package offers a comprehensive set of features for managing authentication operations in a simulated environment. |
12 | 18 |
|
13 | | -```yaml |
14 | | -dependencies: |
15 | | - auth_inmemory: |
16 | | - git: |
17 | | - url: https://github.com/flutter-news-app-full-source-code/auth-inmemory |
18 | | -``` |
| 19 | +<details> |
| 20 | +<summary><strong>🧱 Core Functionality</strong></summary> |
19 | 21 |
|
20 | | -### Features |
| 22 | +### 🚀 `AuthClient` Implementation |
| 23 | +- **`AuthInmemory` Class:** A concrete in-memory implementation of the `AuthClient` interface, providing a standardized way to simulate authentication. |
| 24 | +- **Simulated Authentication Flows:** Implements `requestSignInCode`, `verifySignInCode`, `signInAnonymously`, and `signOut` to simulate various authentication processes. |
| 25 | +- **Reactive State Changes:** Provides `authStateChanges` (a stream that emits the current authenticated `User` or `null` on state changes) and `getCurrentUser` to retrieve the current user. |
21 | 26 |
|
22 | | -This package implements the `AuthClient` interface, providing the following in-memory simulated authentication methods: |
| 27 | +### 🌐 Debugging & Validation |
| 28 | +- **Privileged Flow Simulation: ** Supports an `isDashboardLogin` flag in `requestSignInCode` and `verifySignInCode` to simulate privileged flows, allowing testing of admin-specific authentication logic (e.g., only `[email protected]` is allowed). |
| 29 | +- **Token Retrieval:** Includes a `currentToken` getter to retrieve the simulated authentication token for inspection during development. |
23 | 30 |
|
24 | | -* `authStateChanges`: A stream that emits the current authenticated `User` or `null` on state changes. |
25 | | -* `getCurrentUser`: Retrieves the currently authenticated `User`. |
26 | | -* `requestSignInCode`: Simulates sending a sign-in code to an email. This method supports an optional `isDashboardLogin` flag. When `true`, it simulates a privileged flow where only `[email protected]` is allowed to request a code; otherwise, it throws an `UnauthorizedException`. |
27 | | -* `verifySignInCode`: Simulates verifying a sign-in code and authenticating a user. This method also supports an optional `isDashboardLogin` flag. When `true`, it simulates a privileged flow where only `[email protected]` can successfully verify a code (throwing a `NotFoundException` for other emails) and the authenticated user is assigned the `UserRoles.admin` role. |
28 | | -* `signInAnonymously`: Simulates signing in a user anonymously. |
29 | | -* `signOut`: Simulates signing out the current user. |
30 | | -* `currentToken`: A custom getter to retrieve the simulated authentication token. |
31 | | - |
32 | | -### Usage |
33 | | - |
34 | | -Here's how you can use `AuthInmemory` in your application for demo or testing environments: |
35 | | - |
36 | | -```dart |
37 | | -import 'package:auth_inmemory/auth_inmemory.dart'; |
38 | | -import 'package:core/core.dart'; // For User and AuthSuccessResponse |
39 | | -
|
40 | | -void main() async { |
41 | | - final authClient = AuthInmemory(); |
42 | | -
|
43 | | - // Listen to authentication state changes |
44 | | - authClient.authStateChanges.listen((user) { |
45 | | - if (user != null) { |
46 | | - print('User authenticated: ${user.email ?? 'Anonymous'}'); |
47 | | - } else { |
48 | | - print('User signed out.'); |
49 | | - } |
50 | | - }); |
51 | | -
|
52 | | - // Simulate anonymous sign-in |
53 | | - try { |
54 | | - final anonymousAuthResponse = await authClient.signInAnonymously(); |
55 | | - print('Signed in anonymously. User ID: ${anonymousAuthResponse.user.id}'); |
56 | | - print('Current Token: ${authClient.currentToken}'); |
57 | | - } catch (e) { |
58 | | - print('Anonymous sign-in failed: $e'); |
59 | | - } |
60 | | -
|
61 | | - // Simulate email sign-in flow |
62 | | - const testEmail = '[email protected]'; |
63 | | - try { |
64 | | - await authClient.requestSignInCode(testEmail); |
65 | | - print('Sign-in code requested for $testEmail'); |
66 | | -
|
67 | | - // In a real app, the user would input the code received via email |
68 | | - const code = '123456'; // Hardcoded code for in-memory demo |
69 | | -
|
70 | | - final verifiedAuthResponse = |
71 | | - await authClient.verifySignInCode(testEmail, code); |
72 | | - print('Verified sign-in for ${verifiedAuthResponse.user.email}'); |
73 | | - print('Current Token: ${authClient.currentToken}'); |
74 | | - } on InvalidInputException catch (e) { |
75 | | - print('Invalid input: ${e.message}'); |
76 | | - } on AuthenticationException catch (e) { |
77 | | - print('Authentication failed: ${e.message}'); |
78 | | - } catch (e) { |
79 | | - print('Sign-in failed: $e'); |
80 | | - } |
81 | | -
|
82 | | - // Get current user |
83 | | - final currentUser = await authClient.getCurrentUser(); |
84 | | - print('Current user (after operations): ${currentUser?.email}'); |
85 | | -
|
86 | | - // Simulate sign-out |
87 | | - try { |
88 | | - await authClient.signOut(); |
89 | | - print('User signed out successfully.'); |
90 | | - } catch (e) { |
91 | | - print('Sign-out failed: $e'); |
92 | | - } |
93 | | -} |
94 | | -``` |
| 31 | +### 🛡️ Standardized Error Handling |
| 32 | +- **`HttpException` Propagation:** Throws standard `HttpException` subtypes (e.g., `UnauthorizedException`, `NotFoundException`, `AuthenticationException`, `InvalidInputException`) from `core` on simulated failures, ensuring consistent error handling in a testing context. |
95 | 33 |
|
| 34 | +> **💡 Your Advantage:** This package provides a reliable, in-memory authentication client that simplifies testing and development of authentication-related features. It eliminates the need for external backend dependencies during development, offering immediate feedback and consistent behavior for your authentication logic, especially for complex scenarios like privileged logins. |
96 | 35 |
|
| 36 | +</details> |
97 | 37 |
|
98 | 38 | ## 🔑 Licensing |
99 | 39 |
|
100 | | -This package is source-available and licensed under the [PolyForm Free Trial 1.0.0](LICENSE). Please review the terms before use. |
101 | | - |
102 | | -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. |
| 40 | +This `auth_inmemory` 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