Skip to main content

Authentication Repositories

Repositories provide an abstraction layer for data operations, implementing the Repository pattern from Clean Architecture. They coordinate between data sources and use cases.

AuthRepository (Interface)

Defines the contract for authentication data operations. Location: lib/feature/auth/core/repositories/auth_repository.dart:6

Methods


AuthRepositoryImpl

Concrete implementation of AuthRepository that coordinates between remote and local data sources. Location: lib/feature/auth/data/repositories/auth_repository_impl.dart:11

Constructor

remoteDataSource
AuthRemoteDataSource
required
Handles remote API calls
localDataSource
AuthLocalDataSource
required
Handles local storage operations (tokens, user data)
AuthRepositoryImpl({
  required AuthRemoteDataSource remoteDataSource,
  required AuthLocalDataSource localDataSource,
});

Implementation Details

Usage Example

// Dependency injection setup
final remoteDataSource = AuthRemoteDataSourceImpl(client: httpClient);
final localDataSource = AuthLocalDataSourceImpl(storage: secureStorage);

final authRepository = AuthRepositoryImpl(
  remoteDataSource: remoteDataSource,
  localDataSource: localDataSource,
);

// Using the repository
final result = await authRepository.login(
  'user@example.com',
  'password123',
);

result.fold(
  (failure) => print('Login failed: ${failure.message}'),
  (token) => print('Login successful: $token'),
);

Data Sources

The repository implementation depends on two data sources:

AuthRemoteDataSource

Location: lib/feature/auth/data/datasources/auth_remote_datasource.dart Handles all remote API calls for authentication operations.

AuthLocalDataSource

Location: lib/feature/auth/data/datasources/auth_local_datasource.dart Handles local storage operations for:
  • Authentication tokens
  • User data caching
  • Session persistence

Architecture Notes

  • Separation of Concerns: The repository coordinates between remote API calls and local storage
  • Error Handling: All methods use the Either type for functional error handling
  • Token Management: Tokens are automatically stored locally after successful login
  • Session Cleanup: Failed authentication checks trigger automatic cleanup of local data
  • Type Safety: Strong typing ensures compile-time safety for all operations