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
.
Add the package to your pubspec.yaml
:
dependencies:
auth-repository:
git:
url: https://github.com/flutter-news-app-full-source-code/auth-repository.git
## Features
- Abstracts authentication logic from the UI/business logic layers.
- Provides methods for a complete authentication lifecycle:
- `authStateChanges`: Stream of user authentication state.
- `getCurrentUser`: Retrieves the current authenticated user.
- `requestSignInCode`: Initiates email+code sign-in.
- `verifySignInCode`: Verifies the code, saves the auth token, and returns the user.
- `signInAnonymously`: Signs in anonymously, saves the auth token, and returns the user.
- `signOut`: Signs out the user and clears the auth token.
- Manages authentication token persistence internally using `KvStorageService`.
- Exposes `saveAuthToken(String token)`, `getAuthToken()`, and `clearAuthToken()` for direct token manipulation if needed, but these are typically handled by the main auth flow methods.
- Propagates standardized `HttpException`s from the underlying client and `StorageException`s from the storage service.
## Usage
Instantiate `AuthRepository` by providing implementations of `AuthClient` and `KvStorageService`:
```dart
import 'package:auth_client/auth_client.dart';
import 'package:auth_repository/auth_repository.dart';
import 'package:kv_storage_service/kv_storage_service.dart';
// Assume ConcreteAuthClient is an implementation of AuthClient
final authClient = ConcreteAuthClient(...);
// Assume ConcreteKVStorageService is an implementation of KvStorageService
final storageService = ConcreteKVStorageService(...);
final authRepository = AuthRepository(
authClient: authClient,
storageService: storageService,
);
// Example usage:
authRepository.authStateChanges.listen((user) {
// Handle auth state changes
});
try {
await authRepository.requestSignInCode('test@example.com');
// Handle success
} on InvalidInputException catch (e) {
// Handle invalid email
} catch (e) {
// Handle other errors
}
// Example usage:
try {
final user = await authRepository.verifySignInCode('test@example.com', '123456');
// User is signed in, token is saved automatically.
print('User signed in: ${user.id}');
} on AuthenticationException catch (e) {
// Handle invalid code
} on StorageException catch (e) {
// Handle failure to save token
} catch (e) {
// Handle other errors
}
// Example of anonymous sign-in:
try {
final anonUser = await authRepository.signInAnonymously();
// User is signed in anonymously, token is saved automatically.
print('Anonymous user signed in: ${anonUser.id}');
} catch (e) {
// Handle errors
}
// Example of sign-out:
try {
await authRepository.signOut();
// User is signed out, token is cleared automatically.
print('User signed out.');
} catch (e) {
// Handle errors
}
// Direct token access (e.g., for HTTP client interceptors):
Future<String?> getTokenForHttpClient() async {
final token = await authRepository.getAuthToken();
print('Retrieved token for HTTP client: $token');
return token;
}
This package is source-available and licensed under the PolyForm Free Trial 1.0.0. Please review the terms before use.
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 organization.