feat: refactor code
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import '../../../../../core/storage/token_storage.dart';
|
||||
|
||||
/// ذخیرهی محلیِ توکن احراز هویت.
|
||||
class AuthLocalData {
|
||||
final TokenStorage _storage;
|
||||
AuthLocalData(this._storage);
|
||||
|
||||
Future<void> saveToken(String token) => _storage.write(token);
|
||||
Future<String?> readToken() => _storage.read();
|
||||
Future<void> clearToken() => _storage.clear();
|
||||
|
||||
Future<bool> hasToken() async {
|
||||
final t = await _storage.read();
|
||||
return t != null && t.isNotEmpty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../../../core/locator/locator.dart';
|
||||
import '../../../../../core/network/api_provider_imp.dart';
|
||||
|
||||
/// تماسهای خامِ HTTP مربوط به احراز هویت (خروجی Response).
|
||||
class AuthApiProvider {
|
||||
ApiProviderImp get _api => locator<ApiProviderImp>();
|
||||
|
||||
Future<Response> loginOtp(String mobile) =>
|
||||
_api.post('/auth/login-otp', body: {'mobile': mobile});
|
||||
|
||||
Future<Response> checkOtp(String mobile, String token) =>
|
||||
_api.post('/auth/check-otp', body: {'mobile': mobile, 'token': token});
|
||||
|
||||
Future<Response> updateProfile(String firstName, String avatar) =>
|
||||
_api.post('/profile', body: {'first_name': firstName, 'avatar': avatar});
|
||||
|
||||
Future<Response> me() => _api.get('/me');
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import '../../domain/entities/user_entity.dart';
|
||||
|
||||
/// مدلِ دادهی کاربر؛ از JSON ساخته شده و به UserEntity نگاشت میشود.
|
||||
class UserModel extends UserEntity {
|
||||
const UserModel({
|
||||
required super.id,
|
||||
required super.mobile,
|
||||
super.firstName,
|
||||
super.avatar,
|
||||
});
|
||||
|
||||
factory UserModel.fromJson(Map<String, dynamic> j) => UserModel(
|
||||
id: (j['id'] ?? 0) as int,
|
||||
mobile: (j['mobile'] ?? '') as String,
|
||||
firstName: j['first_name'] as String?,
|
||||
avatar: j['avatar'] as String?,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../../core/error/custom_error.dart';
|
||||
import '../../../../core/resources/data_state.dart';
|
||||
import '../../../../core/usecase/use_case.dart';
|
||||
import '../../domain/entities/user_entity.dart';
|
||||
import '../../domain/repository/auth_repository.dart';
|
||||
import '../data_source/local/auth_local_data.dart';
|
||||
import '../data_source/remote/auth_api_provider.dart';
|
||||
import '../model/user_model.dart';
|
||||
|
||||
class AuthRepositoryImpl extends AuthRepository {
|
||||
final AuthApiProvider api;
|
||||
final AuthLocalData local;
|
||||
AuthRepositoryImpl(this.api, this.local);
|
||||
|
||||
@override
|
||||
Future<DataState<String>> loginOtp(String mobile) async {
|
||||
final Response res = await api.loginOtp(mobile);
|
||||
if (res.statusCode == 200) {
|
||||
return const DataSuccess('ok');
|
||||
}
|
||||
return DataError(errorConvertor(res.statusCode, _msg(res)));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<DataState<UserEntity>> checkOtp(OtpParams params) async {
|
||||
final Response res = await api.checkOtp(params.mobile, params.token);
|
||||
if (res.statusCode == 200) {
|
||||
final token = res.data['token'] as String?;
|
||||
if (token == null || token.isEmpty) {
|
||||
return const DataError('پاسخ نامعتبر از سرور');
|
||||
}
|
||||
await local.saveToken(token);
|
||||
return DataSuccess(UserModel.fromJson(
|
||||
Map<String, dynamic>.from(res.data['user'] as Map)));
|
||||
}
|
||||
return DataError(errorConvertor(res.statusCode, _msg(res)));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<DataState<UserEntity>> updateProfile(ProfileParams params) async {
|
||||
final Response res = await api.updateProfile(params.firstName, params.avatar);
|
||||
if (res.statusCode == 200) {
|
||||
return DataSuccess(UserModel.fromJson(
|
||||
Map<String, dynamic>.from(res.data['user'] as Map)));
|
||||
}
|
||||
return DataError(errorConvertor(res.statusCode, _msg(res)));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<DataState<String>> logout() async {
|
||||
await local.clearToken();
|
||||
return const DataSuccess('ok');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> isLoggedIn() => local.hasToken();
|
||||
|
||||
String? _msg(Response res) {
|
||||
final d = res.data;
|
||||
if (d is Map && d['message'] != null) return d['message'].toString();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user