login
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../../../common/constant/local_storage_key.dart';
|
||||
import '../../../domain/auth/auth.dart';
|
||||
import '../auth_dtos.dart';
|
||||
|
||||
@injectable
|
||||
class AuthLocalDataProvider {
|
||||
final SharedPreferences _sharedPreferences;
|
||||
final String _logName = 'AuthLocalDataProvider';
|
||||
|
||||
AuthLocalDataProvider(this._sharedPreferences);
|
||||
|
||||
Future<void> saveToken(String token) async {
|
||||
await _sharedPreferences.setString(LocalStorageKey.token, token);
|
||||
}
|
||||
|
||||
Future<String?> getToken() async {
|
||||
return _sharedPreferences.getString(LocalStorageKey.token);
|
||||
}
|
||||
|
||||
Future<void> deleteToken() async {
|
||||
await _sharedPreferences.remove(LocalStorageKey.token);
|
||||
}
|
||||
|
||||
Future<bool> hasToken() async {
|
||||
return _sharedPreferences.containsKey(LocalStorageKey.token);
|
||||
}
|
||||
|
||||
Future<void> saveCurrentUser(UserDto user) async {
|
||||
final userJsonString = jsonEncode(user.toJson());
|
||||
await _sharedPreferences.setString(LocalStorageKey.user, userJsonString);
|
||||
}
|
||||
|
||||
Future<User> currentUser() async {
|
||||
final userString = _sharedPreferences.getString(LocalStorageKey.user);
|
||||
if (userString == null) return User.empty();
|
||||
|
||||
final Map<String, dynamic> userMap = jsonDecode(userString);
|
||||
final userDto = UserDto.fromJson(userMap);
|
||||
return userDto.toDomain();
|
||||
}
|
||||
|
||||
Future<void> deleteCurrentUser() async {
|
||||
await _sharedPreferences.remove(LocalStorageKey.user);
|
||||
}
|
||||
|
||||
Future<void> deleteAllAuth() async {
|
||||
try {
|
||||
await _sharedPreferences.remove(LocalStorageKey.token);
|
||||
await _sharedPreferences.remove(LocalStorageKey.user);
|
||||
} catch (e) {
|
||||
log('deleteAllAuthError', name: _logName, error: e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:data_channel/data_channel.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
|
||||
import '../../../common/api/api_client.dart';
|
||||
import '../../../common/api/api_failure.dart';
|
||||
import '../../../common/url/api_path.dart';
|
||||
import '../../../domain/auth/auth.dart';
|
||||
import '../auth_dtos.dart';
|
||||
|
||||
@injectable
|
||||
class AuthRemoteDataProvider {
|
||||
final ApiClient _apiClient;
|
||||
final String _logName = 'AuthRemoteDataProvider';
|
||||
|
||||
AuthRemoteDataProvider(this._apiClient);
|
||||
|
||||
Future<DC<AuthFailure, LoginDto>> login({
|
||||
required String email,
|
||||
required String password,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _apiClient.post(
|
||||
ApiPath.login,
|
||||
data: {'email': email, 'password': password},
|
||||
);
|
||||
|
||||
if (response.data['code'] == 401) {
|
||||
return DC.error(
|
||||
AuthFailure.serverError(
|
||||
ApiFailure.unauthorized('Incorrect email or password'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final dto = LoginDto.fromJson(response.data['data']);
|
||||
return DC.data(dto);
|
||||
} on ApiFailure catch (e, s) {
|
||||
log('login', name: _logName, error: e, stackTrace: s);
|
||||
return DC.error(AuthFailure.serverError(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user