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/
126 changes: 32 additions & 94 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,108 +1,46 @@
# auth_api
<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 API</h1>
<p><strong>A concrete API implementation of the `AuthClient` interface for the Flutter News App Toolkit.</strong></p>
</div>

![coverage: 98%](https://img.shields.io/badge/coverage-98-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-_%25-red?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>

Concrete API implementation of the `AuthClient` interface defined in
`package:auth_client`. This package provides the logic for interacting
with a backend authentication service via HTTP requests using
`package:http_client`.
This `auth_api` package provides a concrete API implementation of the `AuthClient` interface (defined in `package:auth_client`) within the [**Flutter News App Full Source Code Toolkit**](https://github.com/flutter-news-app-full-source-code). It encapsulates the logic for interacting with a backend authentication service via HTTP requests, leveraging `package:http_client` for underlying communication and standardized error handling. This package is crucial for applications that need to connect to a remote authentication service, ensuring consistent and robust authentication mechanisms across the Flutter mobile app, web dashboard, and Dart Frog backend API.

## Getting Started
## ⭐ Feature Showcase: Robust Backend Authentication Integration

Add this package to your `pubspec.yaml` dependencies:
This package offers a comprehensive set of features for integrating with a backend authentication service.

```yaml
dependencies:
auth_api:
git:
url: https://github.com/flutter-news-app-full-source-code/auth-api.git
# Optionally specify a ref (branch, tag, commit hash)
# ref: main
```
<details>
<summary><strong>🧱 Core Functionality</strong></summary>

You also need to include `http_client` and `auth_client` (which this
package depends on).
### 🚀 `AuthClient` Implementation
- **`AuthApi` Class:** A concrete implementation of the `AuthClient` interface, providing a standardized way to interact with a remote authentication service.
- **HTTP-Based Communication:** Utilizes `package:http_client` for making HTTP requests to the backend authentication service.

## Features
### 🔐 Authentication Flows
- **`requestSignInCode`:** Initiates the process of requesting a sign-in code via email.
- **`verifySignInCode`:** Verifies the sign-in code to complete the authentication process.
- **`signInAnonymously`:** Allows users to sign in anonymously, creating a temporary user identity on the backend.
- **`getCurrentUser`:** Retrieves the currently authenticated user's details from the backend.
- **`authStateChanges` Stream:** Provides a stream for monitoring real-time authentication state changes.
- **`signOut`:** Handles signing out the current user from the backend.

Provides an `AuthApi` class implementing `AuthClient` with the following
capabilities:
### 🛡️ Standardized Error Handling
- **`HttpException` Propagation:** Propagates standardized `HttpException` errors (from `core`) from the underlying `http_client`, ensuring consistent and predictable error management across the application layers.

* Requesting a sign-in code via email (`requestSignInCode`).
* Verifying the sign-in code to complete authentication (`verifySignInCode`).
* Signing in anonymously (`signInAnonymously`).
* Retrieving the current authenticated user (`getCurrentUser`).
* Monitoring authentication state changes via a stream (`authStateChanges`).
* Signing out the current user (`signOut`).

## Usage

Instantiate `AuthApi` with a configured `HttpClient` instance:

```dart
import 'package:auth_api/auth_api.dart';
import 'package:auth_client/auth_client.dart';
import 'package:http_client/http_client.dart';
void main() async {
// Configure HttpClient (replace with your actual base URL and token logic)
final httpClient = HttpClient(
baseUrl: 'https://your-api.com',
tokenProvider: () async => 'YOUR_AUTH_TOKEN', // Or null if not logged in
);
// Create the auth API client
final AuthClient authClient = AuthApi(httpClient: httpClient);
// Listen to authentication state changes
authClient.authStateChanges.listen((user) {
if (user != null) {
// To check for an anonymous user, you would typically check their role,
// e.g., if (user.role == UserRole.guestUser) { ... }
// Assuming UserRole enum is accessible/imported.
print('User signed in: ${user.id}, Role: ${user.role}');
} else {
print('User signed out.');
}
});
try {
// Example: Request sign-in code
await authClient.requestSignInCode('user@example.com');
print('Sign-in code requested.');
// Example: Verify code (replace '123456' with actual code)
// final authResponse = await authClient.verifySignInCode('user@example.com', '123456');
// print('User verified: ${authResponse.user.id}, Token: ${authResponse.token}');
// Example: Sign in anonymously
// final anonAuthResponse = await authClient.signInAnonymously();
// print('Signed in anonymously: ${anonAuthResponse.user.id}, Token: ${anonAuthResponse.token}');
// Example: Get current user
// final currentUser = await authClient.getCurrentUser();
// if (currentUser != null) { ... }
// Example: Sign out
// await authClient.signOut();
} on HttpException catch (e) {
print('Authentication error: $e');
} finally {
// Remember to dispose the client if it has resources like streams
// (In this specific implementation, AuthApi has a dispose method)
(authClient as AuthApi).dispose();
}
}
```
### 💉 Dependency Injection Ready
- **`HttpClient` Dependency:** Requires a configured `HttpClient` instance (from `package:http_client`) via its constructor, promoting loose coupling and testability.

> **💡 Your Advantage:** This package provides a robust and production-ready API client for backend authentication. It simplifies the integration of remote authentication services, leverages standardized error handling, and ensures consistent authentication flows across your application ecosystem.
</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_api` 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.
2 changes: 1 addition & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include: package:very_good_analysis/analysis_options.7.0.0.yaml
include: package:very_good_analysis/analysis_options.9.0.0.yaml
analyzer:
errors:
avoid_print: ignore
Expand Down
76 changes: 0 additions & 76 deletions coverage/lcov.info

This file was deleted.

Loading
Loading