Init login feature add data layer
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
import 'package:model/model.dart';
|
||||
|
||||
abstract class AuthenticationDataSource {
|
||||
Future<User> authenticationUser(Uri baseUrl, UserName userName, Password password);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import 'package:model/model.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/datasource/atuthentitcation_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/model/request/account_request.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/model/response/user_response.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/network/login_api.dart';
|
||||
|
||||
class AuthenticationDataSourceImpl extends AuthenticationDataSource {
|
||||
|
||||
final LoginAPI loginAPI;
|
||||
|
||||
AuthenticationDataSourceImpl(this.loginAPI);
|
||||
|
||||
@override
|
||||
Future<User> authenticationUser(Uri baseUrl, UserName userName, Password password) {
|
||||
return Future.sync(() async {
|
||||
final userResponse = await loginAPI.authenticationUser(baseUrl, AccountRequest(userName, password));
|
||||
return userResponse.toUser();
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:model/model.dart';
|
||||
|
||||
class UserIdConverter implements JsonConverter<UserId, String> {
|
||||
const UserIdConverter();
|
||||
|
||||
@override
|
||||
UserId fromJson(String json) => UserId(json);
|
||||
|
||||
@override
|
||||
String toJson(UserId object) => jsonEncode(object.id);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:model/model.dart';
|
||||
|
||||
class AccountRequest with EquatableMixin {
|
||||
final UserName userName;
|
||||
final Password password;
|
||||
|
||||
AccountRequest(this.userName, this.password);
|
||||
|
||||
Map<String, dynamic> toJson() => <String, dynamic>{
|
||||
'username': userName.userName,
|
||||
'password': password.value,
|
||||
};
|
||||
|
||||
@override
|
||||
List<Object> get props => [userName, password];
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import 'package:core/data/constants/constant.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:model/model.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/model/converter/user_id_converter.dart';
|
||||
|
||||
part 'user_response.g.dart';
|
||||
|
||||
@UserIdConverter()
|
||||
@JsonSerializable()
|
||||
class UserResponse with EquatableMixin {
|
||||
|
||||
@JsonKey(name: Constant.userId)
|
||||
final UserId userId;
|
||||
@JsonKey(name: Constant.firstName)
|
||||
final String firstName;
|
||||
@JsonKey(name: Constant.lastName)
|
||||
final String lastName;
|
||||
|
||||
UserResponse(
|
||||
this.userId,
|
||||
this.firstName,
|
||||
this.lastName
|
||||
);
|
||||
|
||||
factory UserResponse.fromJson(Map<String, dynamic> json) => _$UserResponseFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$UserResponseToJson(this);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
userId,
|
||||
firstName,
|
||||
lastName,
|
||||
];
|
||||
}
|
||||
|
||||
extension UserResponseExtension on UserResponse {
|
||||
User toUser() {
|
||||
return User(
|
||||
userId,
|
||||
firstName,
|
||||
lastName,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:core/core.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/model/request/account_request.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/model/response/user_response.dart';
|
||||
|
||||
class LoginAPI {
|
||||
final DioClient _dioClient;
|
||||
|
||||
LoginAPI(this._dioClient);
|
||||
|
||||
Future<UserResponse> authenticationUser(Uri baseUrl, AccountRequest accountRequest) async {
|
||||
final basicAuth = 'Basic ' + base64Encode(utf8.encode('${accountRequest.userName.userName}:${accountRequest.password.value}'));
|
||||
final resultJson = await _dioClient.post(
|
||||
Endpoint.authentication.generateAuthenticationUrl(baseUrl),
|
||||
options: Options(headers: _buildHeaderRequestParam(basicAuth)),
|
||||
data: accountRequest.toJson());
|
||||
return UserResponse.fromJson(resultJson);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _buildHeaderRequestParam(String authorizationHeader) {
|
||||
final headerParam = _dioClient.getHeaders();
|
||||
headerParam[HttpHeaders.authorizationHeader] = authorizationHeader;
|
||||
return headerParam;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import 'package:model/model.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/datasource/atuthentitcation_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/repository/authentication_repository.dart';
|
||||
|
||||
class AuthenticationRepositoryImpl extends AuthenticationRepository {
|
||||
final AuthenticationDataSource loginDataSource;
|
||||
|
||||
AuthenticationRepositoryImpl(this.loginDataSource);
|
||||
|
||||
@override
|
||||
Future<User> authenticationUser(Uri baseUrl, UserName userName, Password password) {
|
||||
return Future.value(User(UserId(userName.userName), "Alice", "Alice"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import 'package:model/model.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/utils/login_constant.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
|
||||
|
||||
class CredentialRepositoryImpl extends CredentialRepository {
|
||||
|
||||
final SharedPreferences sharedPreferences;
|
||||
|
||||
CredentialRepositoryImpl(this.sharedPreferences);
|
||||
|
||||
@override
|
||||
Future<Uri> getBaseUrl() async {
|
||||
return Uri.parse(sharedPreferences.getString(LoginConstant.keyBaseUrl) ?? '');
|
||||
}
|
||||
|
||||
@override
|
||||
Future saveBaseUrl(Uri baseUrl) async {
|
||||
await sharedPreferences.setString(LoginConstant.keyBaseUrl, baseUrl.origin);
|
||||
}
|
||||
|
||||
@override
|
||||
Future removeBaseUrl() async {
|
||||
await sharedPreferences.remove(LoginConstant.keyBaseUrl);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Password> getPassword() async {
|
||||
return Password(sharedPreferences.getString(LoginConstant.keyPassword) ?? '');
|
||||
}
|
||||
|
||||
@override
|
||||
Future removePassword() async {
|
||||
await sharedPreferences.remove(LoginConstant.keyPassword);
|
||||
}
|
||||
|
||||
@override
|
||||
Future savePassword(Password password) async {
|
||||
await sharedPreferences.setString(LoginConstant.keyPassword, password.value);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<UserName> getUserName() async {
|
||||
return UserName(sharedPreferences.getString(LoginConstant.keyUserName) ?? '');
|
||||
}
|
||||
|
||||
@override
|
||||
Future removeUserName() async {
|
||||
await sharedPreferences.remove(LoginConstant.keyUserName);
|
||||
}
|
||||
|
||||
@override
|
||||
Future saveUserName(UserName userName) async {
|
||||
await sharedPreferences.setString(LoginConstant.keyUserName, userName.userName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class LoginConstant {
|
||||
static const keyBaseUrl = 'KEY_BASE_URL';
|
||||
static const keyUserName = 'KEY_USER_NAME';
|
||||
static const keyPassword = 'KEY_PASSWORD';
|
||||
}
|
||||
Reference in New Issue
Block a user