update api

This commit is contained in:
Phuoc Nguyen
2025-10-10 17:15:40 +07:00
parent b94c158004
commit 04f7042b8d
24 changed files with 3322 additions and 8 deletions

View File

@@ -0,0 +1,175 @@
import 'package:dartz/dartz.dart';
import '../../../../core/errors/exceptions.dart';
import '../../../../core/errors/failures.dart';
import '../../../../core/network/dio_client.dart';
import '../../../../core/storage/secure_storage.dart';
import '../../domain/entities/auth_response.dart';
import '../../domain/entities/user.dart';
import '../../domain/repositories/auth_repository.dart';
import '../datasources/auth_remote_datasource.dart';
import '../models/login_dto.dart';
import '../models/register_dto.dart';
/// Implementation of AuthRepository
class AuthRepositoryImpl implements AuthRepository {
final AuthRemoteDataSource remoteDataSource;
final SecureStorage secureStorage;
final DioClient dioClient;
AuthRepositoryImpl({
required this.remoteDataSource,
required this.secureStorage,
required this.dioClient,
});
@override
Future<Either<Failure, AuthResponse>> login({
required String email,
required String password,
}) async {
try {
final loginDto = LoginDto(email: email, password: password);
final authResponse = await remoteDataSource.login(loginDto);
// Save token to secure storage
await secureStorage.saveAccessToken(authResponse.accessToken);
// Set token in Dio client for subsequent requests
dioClient.setAuthToken(authResponse.accessToken);
return Right(authResponse);
} on InvalidCredentialsException catch (e) {
return Left(InvalidCredentialsFailure(e.message));
} on UnauthorizedException catch (e) {
return Left(UnauthorizedFailure(e.message));
} on ValidationException catch (e) {
return Left(ValidationFailure(e.message));
} on NetworkException catch (e) {
return Left(NetworkFailure(e.message));
} on ServerException catch (e) {
return Left(ServerFailure(e.message));
} catch (e) {
return Left(ServerFailure('Unexpected error: $e'));
}
}
@override
Future<Either<Failure, AuthResponse>> register({
required String name,
required String email,
required String password,
List<String> roles = const ['user'],
}) async {
try {
final registerDto = RegisterDto(
name: name,
email: email,
password: password,
roles: roles,
);
final authResponse = await remoteDataSource.register(registerDto);
// Save token to secure storage
await secureStorage.saveAccessToken(authResponse.accessToken);
// Set token in Dio client for subsequent requests
dioClient.setAuthToken(authResponse.accessToken);
return Right(authResponse);
} on ConflictException catch (e) {
return Left(ConflictFailure(e.message));
} on ValidationException catch (e) {
return Left(ValidationFailure(e.message));
} on NetworkException catch (e) {
return Left(NetworkFailure(e.message));
} on ServerException catch (e) {
return Left(ServerFailure(e.message));
} catch (e) {
return Left(ServerFailure('Unexpected error: $e'));
}
}
@override
Future<Either<Failure, User>> getProfile() async {
try {
final user = await remoteDataSource.getProfile();
return Right(user);
} on UnauthorizedException catch (e) {
return Left(UnauthorizedFailure(e.message));
} on TokenExpiredException catch (e) {
return Left(TokenExpiredFailure(e.message));
} on NetworkException catch (e) {
return Left(NetworkFailure(e.message));
} on ServerException catch (e) {
return Left(ServerFailure(e.message));
} catch (e) {
return Left(ServerFailure('Unexpected error: $e'));
}
}
@override
Future<Either<Failure, AuthResponse>> refreshToken() async {
try {
final authResponse = await remoteDataSource.refreshToken();
// Update token in secure storage
await secureStorage.saveAccessToken(authResponse.accessToken);
// Update token in Dio client
dioClient.setAuthToken(authResponse.accessToken);
return Right(authResponse);
} on UnauthorizedException catch (e) {
return Left(UnauthorizedFailure(e.message));
} on TokenExpiredException catch (e) {
return Left(TokenExpiredFailure(e.message));
} on NetworkException catch (e) {
return Left(NetworkFailure(e.message));
} on ServerException catch (e) {
return Left(ServerFailure(e.message));
} catch (e) {
return Left(ServerFailure('Unexpected error: $e'));
}
}
@override
Future<Either<Failure, void>> logout() async {
try {
// Clear token from secure storage
await secureStorage.deleteAllTokens();
// Clear token from Dio client
dioClient.clearAuthToken();
return const Right(null);
} catch (e) {
return Left(CacheFailure('Failed to logout: $e'));
}
}
@override
Future<bool> isAuthenticated() async {
try {
final hasToken = await secureStorage.hasAccessToken();
if (hasToken) {
final token = await secureStorage.getAccessToken();
if (token != null) {
dioClient.setAuthToken(token);
return true;
}
}
return false;
} catch (e) {
return false;
}
}
@override
Future<String?> getAccessToken() async {
try {
return await secureStorage.getAccessToken();
} catch (e) {
return null;
}
}
}