Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# See https://www.dartlang.org/guides/libraries/private-files

# Files and directories created by pub
.dart_tool/
.packages
build/
pubspec.lock
coverage/
124 changes: 28 additions & 96 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,110 +1,42 @@
# auth_repository
<div align="center">
<img src="https://avatars.githubusercontent.com/u/202675624?s=400&u=dc72a2b53e8158956a3b672f8e52e39394b6b610&v=4" alt="Flutter News App Toolkit Logo" width="220">
<h1>Auth Repository</h1>
<p><strong>A repository that provides an abstraction layer over authentication operations for the Flutter News App Toolkit.</strong></p>
</div>

![coverage: percentage](https://img.shields.io/badge/coverage-100-green)
[![style: very good analysis](https://img.shields.io/badge/style-very_good_analysis-B22C89.svg)](https://pub.dev/packages/very_good_analysis)
[![License: PolyForm Free Trial](https://img.shields.io/badge/License-PolyForm%20Free%20Trial-blue)](https://polyformproject.org/licenses/free-trial/1.0.0)
<p align="center">
<img src="https://img.shields.io/badge/coverage-100%25-green?style=for-the-badge" alt="coverage">
<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>
<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>
</p>

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`.
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.

## Getting Started
## ⭐ Feature Showcase: Business-Focused Authentication Management

Add the package to your `pubspec.yaml`:
This package offers a comprehensive set of features for managing authentication operations.

```yaml
dependencies:
auth-repository:
git:
url: https://github.com/flutter-news-app-full-source-code/auth-repository.git
<details>
<summary><strong>🧱 Core Functionality</strong></summary>

## Features
### 🚀 `AuthRepository` Class
- **`AuthRepository`:** Provides a clean, business-focused interface for authentication-related tasks, abstracting the underlying `AuthClient` implementation.
- **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).

- 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.
### 🌐 Token Persistence Management
- **`KvStorageService` Integration:** Manages authentication token persistence internally using an injected `KvStorageService`.
- **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.

## Usage
### 🛡️ Standardized Error Handling
- **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.

Instantiate `AuthRepository` by providing implementations of `AuthClient` and `KvStorageService`:
### 💉 Dependency Injection Ready
- **`AuthClient` & `KvStorageService` Dependencies:** Requires instances of `AuthClient` (from `auth_client`) and `KvStorageService` (from `kv_storage_service`) via its constructor, promoting loose coupling and testability.

```dart
import 'package:auth_client/auth_client.dart';
import 'package:auth_repository/auth_repository.dart';
import 'package:kv_storage_service/kv_storage_service.dart';
> **💡 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.

// 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;
}
```
</details>

## 🔑 Licensing

This package is source-available and licensed under the [PolyForm Free Trial 1.0.0](LICENSE). 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**](https://github.com/flutter-news-app-full-source-code) organization.
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.
3 changes: 2 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include: package:very_good_analysis/analysis_options.6.0.0.yaml
include: package:very_good_analysis/analysis_options.9.0.0.yaml
analyzer:
errors:
lines_longer_than_80_chars: ignore
document_ignores: ignore
43 changes: 0 additions & 43 deletions coverage/lcov.info

This file was deleted.

Loading
Loading