first commit
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:frontend_eccp_mobile/app/core/config/app_environment.dart';
|
||||
import 'package:frontend_eccp_mobile/app/core/network/api_endpoint.dart';
|
||||
import 'package:frontend_eccp_mobile/app/core/utils/session_manager.dart';
|
||||
import 'package:frontend_eccp_mobile/modules/auth/refresh_token/data/model/refresh_token_response.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
|
||||
@lazySingleton
|
||||
class AuthRemoteDataSource {
|
||||
final Dio _dio = Dio(
|
||||
BaseOptions(
|
||||
baseUrl: AppEnvironment.baseUrl,
|
||||
),
|
||||
);
|
||||
|
||||
Future<RefreshTokenResponse> refreshToken() async {
|
||||
final token = await SessionManager.getRefreshToken();
|
||||
final response = await _dio.post<Map<String, dynamic>>(
|
||||
ApiEndpoint.refreshToken,
|
||||
data: {'refresh_token': token},
|
||||
);
|
||||
|
||||
final data = response.data;
|
||||
if (data == null) {
|
||||
throw Exception('EMPTY_RESPONSE');
|
||||
}
|
||||
|
||||
return RefreshTokenResponse.fromJson(data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
class RefreshTokenResponse {
|
||||
RefreshTokenResponse({
|
||||
required this.status,
|
||||
required this.message,
|
||||
required this.accessToken,
|
||||
required this.refreshToken,
|
||||
required this.tokenType,
|
||||
required this.expiresIn,
|
||||
});
|
||||
|
||||
factory RefreshTokenResponse.fromJson(Map<String, dynamic> json) {
|
||||
final data = json['data'] as Map<String, dynamic>? ?? {};
|
||||
|
||||
return RefreshTokenResponse(
|
||||
status: json['status']?.toString() ?? '',
|
||||
message: json['message']?.toString() ?? '',
|
||||
accessToken: data['access_token']?.toString() ?? '',
|
||||
refreshToken: data['refresh_token']?.toString() ?? '',
|
||||
tokenType: data['token_type']?.toString() ?? '',
|
||||
expiresIn: int.tryParse(data['expires_in']?.toString() ?? '0') ?? 0,
|
||||
);
|
||||
}
|
||||
|
||||
final String status;
|
||||
final String message;
|
||||
final String accessToken;
|
||||
final String refreshToken;
|
||||
final String tokenType;
|
||||
final int expiresIn;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import 'package:frontend_eccp_mobile/modules/auth/refresh_token/data/datasources/auth_remote_datasource.dart';
|
||||
import 'package:frontend_eccp_mobile/modules/auth/refresh_token/data/model/refresh_token_response.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
|
||||
@lazySingleton
|
||||
class AuthRepository {
|
||||
AuthRepository(this._remoteDatasource);
|
||||
|
||||
final AuthRemoteDataSource _remoteDatasource;
|
||||
|
||||
Future<RefreshTokenResponse> refreshToken() async {
|
||||
final response = await _remoteDatasource.refreshToken();
|
||||
|
||||
if (response.status != 'success') {
|
||||
throw Exception(response.message);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user