|
| 1 | +# ht_auth_inmemory |
| 2 | + |
| 3 | + |
| 4 | +[](https://pub.dev/packages/very_good_analysis) |
| 5 | +[](https://polyformproject.org/licenses/free-trial/1.0.0) |
| 6 | + |
| 7 | +An in-memory implementation of the `HtAuthClient` 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. |
| 8 | + |
| 9 | +### Getting Started |
| 10 | + |
| 11 | +Add the following to your `pubspec.yaml` dependencies: |
| 12 | + |
| 13 | +```yaml |
| 14 | +dependencies: |
| 15 | + ht_auth_inmemory: |
| 16 | + git: |
| 17 | + url: https://github.com/headlines-toolkit/ht-auth-inmemory |
| 18 | +``` |
| 19 | +
|
| 20 | +### Features |
| 21 | +
|
| 22 | +This package implements the `HtAuthClient` interface, providing the following in-memory simulated authentication methods: |
| 23 | + |
| 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. |
| 27 | +* `verifySignInCode`: Simulates verifying a sign-in code and authenticating a user. |
| 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 `HtAuthInmemory` in your application for demo or testing environments: |
| 35 | + |
| 36 | +```dart |
| 37 | +import 'package:ht_auth_inmemory/ht_auth_inmemory.dart'; |
| 38 | +import 'package:ht_shared/ht_shared.dart'; // For User and AuthSuccessResponse |
| 39 | +
|
| 40 | +void main() async { |
| 41 | + final authClient = HtAuthInmemory(); |
| 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 | +``` |
| 95 | + |
| 96 | +### License |
| 97 | + |
| 98 | +This package is licensed under the [PolyForm Free Trial](LICENSE). Please review the terms before use. |
0 commit comments