first commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:frontend_eccp_mobile/app/core/constants/dio_constant.dart';
|
||||
import 'package:frontend_eccp_mobile/app/core/network/api_endpoint.dart';
|
||||
import 'package:frontend_eccp_mobile/app/core/network/dio_client.dart';
|
||||
import 'package:frontend_eccp_mobile/modules/auth/login/data/model/login_request.dart';
|
||||
import 'package:frontend_eccp_mobile/modules/auth/login/data/model/login_response.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
|
||||
@lazySingleton
|
||||
class LoginRemoteDataSource {
|
||||
final Dio _dio = DioClient.getInstance();
|
||||
|
||||
Future<LoginResponse> login(LoginRequest request) async {
|
||||
final response = await _dio.post<Map<String, dynamic>>(
|
||||
ApiEndpoint.login,
|
||||
data: request.toJson(),
|
||||
options: Options(
|
||||
extra: {DioExtraKey.noAuth: true},
|
||||
),
|
||||
);
|
||||
|
||||
final data = response.data;
|
||||
if (data == null) {
|
||||
throw Exception('EMPTY_RESPONSE');
|
||||
}
|
||||
|
||||
return LoginResponse.fromJson(data);
|
||||
}
|
||||
}
|
||||
29
lib/modules/auth/login/data/model/login_data.dart
Normal file
29
lib/modules/auth/login/data/model/login_data.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
import 'package:frontend_eccp_mobile/modules/auth/login/data/model/login_user.dart';
|
||||
|
||||
class LoginData {
|
||||
LoginData({
|
||||
required this.accessToken,
|
||||
required this.refreshToken,
|
||||
required this.tokenType,
|
||||
required this.expiresIn,
|
||||
required this.user,
|
||||
});
|
||||
|
||||
factory LoginData.fromJson(Map<String, dynamic> json) {
|
||||
return LoginData(
|
||||
accessToken: json['access_token']?.toString() ?? '',
|
||||
refreshToken: json['refresh_token']?.toString() ?? '',
|
||||
tokenType: json['token_type']?.toString() ?? '',
|
||||
expiresIn: int.tryParse(json['expires_in']?.toString() ?? '0') ?? 0,
|
||||
user: LoginUser.fromJson(
|
||||
json['user'] as Map<String, dynamic>? ?? {},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final String accessToken;
|
||||
final String refreshToken;
|
||||
final String tokenType;
|
||||
final int expiresIn;
|
||||
final LoginUser user;
|
||||
}
|
||||
16
lib/modules/auth/login/data/model/login_request.dart
Normal file
16
lib/modules/auth/login/data/model/login_request.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
class LoginRequest {
|
||||
LoginRequest({
|
||||
required this.nrp,
|
||||
required this.password,
|
||||
});
|
||||
|
||||
final String nrp;
|
||||
final String password;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'nrp': nrp,
|
||||
'password': password,
|
||||
};
|
||||
}
|
||||
}
|
||||
23
lib/modules/auth/login/data/model/login_response.dart
Normal file
23
lib/modules/auth/login/data/model/login_response.dart
Normal file
@@ -0,0 +1,23 @@
|
||||
import 'package:frontend_eccp_mobile/modules/auth/login/data/model/login_data.dart';
|
||||
|
||||
class LoginResponse {
|
||||
LoginResponse({
|
||||
required this.status,
|
||||
required this.message,
|
||||
required this.data,
|
||||
});
|
||||
|
||||
factory LoginResponse.fromJson(Map<String, dynamic> json) {
|
||||
return LoginResponse(
|
||||
status: json['status']?.toString() ?? '',
|
||||
message: json['message']?.toString() ?? '',
|
||||
data: LoginData.fromJson(
|
||||
json['data'] as Map<String, dynamic>? ?? {},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final String status;
|
||||
final String message;
|
||||
final LoginData data;
|
||||
}
|
||||
16
lib/modules/auth/login/data/model/login_user.dart
Normal file
16
lib/modules/auth/login/data/model/login_user.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
class LoginUser {
|
||||
LoginUser({
|
||||
required this.name,
|
||||
required this.nrp,
|
||||
});
|
||||
|
||||
factory LoginUser.fromJson(Map<String, dynamic> json) {
|
||||
return LoginUser(
|
||||
name: json['name']?.toString() ?? '',
|
||||
nrp: json['nrp']?.toString() ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
final String name;
|
||||
final String nrp;
|
||||
}
|
||||
14
lib/modules/auth/login/data/model/user_session.dart
Normal file
14
lib/modules/auth/login/data/model/user_session.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
class UserSession {
|
||||
String? token;
|
||||
Map<String, dynamic>? user;
|
||||
|
||||
bool get isLoggedIn => token != null && token!.isNotEmpty;
|
||||
|
||||
String get name => user?['name']?.toString() ?? '';
|
||||
String get nrp => user?['nrp']?.toString() ?? '';
|
||||
|
||||
void clear() {
|
||||
token = null;
|
||||
user = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import 'package:frontend_eccp_mobile/modules/auth/login/data/datasources/login_remote_datasource.dart';
|
||||
import 'package:frontend_eccp_mobile/modules/auth/login/data/model/login_request.dart';
|
||||
import 'package:frontend_eccp_mobile/modules/auth/login/data/model/login_response.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
|
||||
@lazySingleton
|
||||
class LoginRepository {
|
||||
LoginRepository(this._remoteDatasource);
|
||||
|
||||
final LoginRemoteDataSource _remoteDatasource;
|
||||
|
||||
Future<LoginResponse> login(LoginRequest request) async {
|
||||
return _remoteDatasource.login(request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user