fix : change app architecture

feat : add some method to local storage
This commit is contained in:
2025-05-14 09:42:44 +03:30
parent e11ef1990c
commit feb3df6a06
50 changed files with 1007 additions and 70 deletions

View File

@@ -0,0 +1,14 @@
enum ApiEnvironment {
dam(url: 'https://api.dam.rasadyar.net');
const ApiEnvironment({required this.url});
final String url;
String get baseUrl {
switch (this) {
case ApiEnvironment.dam:
return url;
}
}
}

View File

@@ -0,0 +1,42 @@
import 'package:auths/data/repositories/auth_repository_imp.dart';
import 'package:rasadyar_core/core.dart';
import '../di/auth_di.dart';
import 'constant.dart';
class DioRemoteManager {
DioRemote? _currentClient;
ApiEnvironment? _currentEnv;
DioRemote setEnvironment(ApiEnvironment env) {
if (_currentEnv != env) {
_currentClient = DioRemote(env.baseUrl);
_currentEnv = env;
}
return _currentClient!;
}
DioRemote get currentClient {
if (_currentClient == null) {
throw Exception('Call setEnvironment() before accessing DioRemote.');
}
return _currentClient!;
}
ApiEnvironment? get currentEnv => _currentEnv;
}
Future<void> switchAuthEnvironment(ApiEnvironment env) async {
final manager = diAuth.get<DioRemoteManager>();
final dioRemote = manager.setEnvironment(env);
if (diAuth.isRegistered<AuthRepositoryImpl>()) {
await diAuth.unregister<AuthRepositoryImpl>();
}
diAuth.registerLazySingleton<AuthRepositoryImpl>(
() => AuthRepositoryImpl(dioRemote),
);
}

View File

@@ -0,0 +1,23 @@
import 'package:auths/data/common/constant.dart';
import 'package:auths/data/repositories/auth_repository_imp.dart';
import 'package:auths/data/services/auth_service.dart';
import 'package:rasadyar_core/core.dart';
import '../common/dio_manager.dart';
GetIt diAuth = GetIt.instance;
Future<void> setupAuthDI() async {
diAuth.registerLazySingleton(() => DioRemoteManager());
final manager = diAuth.get<DioRemoteManager>();
final dioRemote = manager.setEnvironment(ApiEnvironment.dam);
diAuth.registerLazySingleton<AuthRepositoryImpl>(
() => AuthRepositoryImpl(dioRemote),
);
diAuth.registerLazySingleton(() => AuthService());
//hive
await diAuth.registerCachedFactoryAsync(() async=>await ,)
}

View File

@@ -0,0 +1,41 @@
import 'package:rasadyar_core/core.dart';
part 'user_local_model.g.dart';
@HiveType(typeId: 0)
class UserLocalModel extends HiveObject {
@HiveField(0)
String? username;
@HiveField(1)
String? password;
@HiveField(2)
String? token;
@HiveField(3)
String? refreshToken;
@HiveField(4)
String? name;
UserLocalModel({
this.username,
this.password,
this.token,
this.refreshToken,
this.name,
});
UserLocalModel copyWith({
String? username,
String? password,
String? token,
String? refreshToken,
String? name,
}) {
return UserLocalModel(
username: username ?? this.username,
password: password ?? this.password,
token: token ?? this.token,
refreshToken: refreshToken ?? this.refreshToken,
name: name ?? this.name,
);
}
}

View File

@@ -0,0 +1,53 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'user_local_model.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class UserLocalModelAdapter extends TypeAdapter<UserLocalModel> {
@override
final typeId = 0;
@override
UserLocalModel read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return UserLocalModel(
username: fields[0] as String?,
password: fields[1] as String?,
token: fields[2] as String?,
refreshToken: fields[3] as String?,
name: fields[4] as String?,
);
}
@override
void write(BinaryWriter writer, UserLocalModel obj) {
writer
..writeByte(5)
..writeByte(0)
..write(obj.username)
..writeByte(1)
..write(obj.password)
..writeByte(2)
..write(obj.token)
..writeByte(3)
..write(obj.refreshToken)
..writeByte(4)
..write(obj.name);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is UserLocalModelAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}

View File

@@ -0,0 +1,21 @@
import 'package:rasadyar_core/core.dart';
part 'login_request_model.freezed.dart';
part 'login_request_model.g.dart';
@freezed
abstract class LoginRequestModel with _$LoginRequestModel {
const factory LoginRequestModel({
String? username,
String? password,
String? captchaCode,
String? captchaKey,
}) = _LoginRequestModel;
factory LoginRequestModel.fromJson(Map<String, dynamic> json) =>
_$LoginRequestModelFromJson(json);
const LoginRequestModel._();
String get formattedCaptchaKey => 'rest_captcha_$captchaKey.0';
}

View File

@@ -0,0 +1,157 @@
// dart format width=80
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
part of 'login_request_model.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
// dart format off
T _$identity<T>(T value) => value;
/// @nodoc
mixin _$LoginRequestModel {
String? get username; String? get password; String? get captchaCode; String? get captchaKey;
/// Create a copy of LoginRequestModel
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@pragma('vm:prefer-inline')
$LoginRequestModelCopyWith<LoginRequestModel> get copyWith => _$LoginRequestModelCopyWithImpl<LoginRequestModel>(this as LoginRequestModel, _$identity);
/// Serializes this LoginRequestModel to a JSON map.
Map<String, dynamic> toJson();
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is LoginRequestModel&&(identical(other.username, username) || other.username == username)&&(identical(other.password, password) || other.password == password)&&(identical(other.captchaCode, captchaCode) || other.captchaCode == captchaCode)&&(identical(other.captchaKey, captchaKey) || other.captchaKey == captchaKey));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType,username,password,captchaCode,captchaKey);
@override
String toString() {
return 'LoginRequestModel(username: $username, password: $password, captchaCode: $captchaCode, captchaKey: $captchaKey)';
}
}
/// @nodoc
abstract mixin class $LoginRequestModelCopyWith<$Res> {
factory $LoginRequestModelCopyWith(LoginRequestModel value, $Res Function(LoginRequestModel) _then) = _$LoginRequestModelCopyWithImpl;
@useResult
$Res call({
String? username, String? password, String? captchaCode, String? captchaKey
});
}
/// @nodoc
class _$LoginRequestModelCopyWithImpl<$Res>
implements $LoginRequestModelCopyWith<$Res> {
_$LoginRequestModelCopyWithImpl(this._self, this._then);
final LoginRequestModel _self;
final $Res Function(LoginRequestModel) _then;
/// Create a copy of LoginRequestModel
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline') @override $Res call({Object? username = freezed,Object? password = freezed,Object? captchaCode = freezed,Object? captchaKey = freezed,}) {
return _then(_self.copyWith(
username: freezed == username ? _self.username : username // ignore: cast_nullable_to_non_nullable
as String?,password: freezed == password ? _self.password : password // ignore: cast_nullable_to_non_nullable
as String?,captchaCode: freezed == captchaCode ? _self.captchaCode : captchaCode // ignore: cast_nullable_to_non_nullable
as String?,captchaKey: freezed == captchaKey ? _self.captchaKey : captchaKey // ignore: cast_nullable_to_non_nullable
as String?,
));
}
}
/// @nodoc
@JsonSerializable()
class _LoginRequestModel extends LoginRequestModel {
const _LoginRequestModel({this.username, this.password, this.captchaCode, this.captchaKey}): super._();
factory _LoginRequestModel.fromJson(Map<String, dynamic> json) => _$LoginRequestModelFromJson(json);
@override final String? username;
@override final String? password;
@override final String? captchaCode;
@override final String? captchaKey;
/// Create a copy of LoginRequestModel
/// with the given fields replaced by the non-null parameter values.
@override @JsonKey(includeFromJson: false, includeToJson: false)
@pragma('vm:prefer-inline')
_$LoginRequestModelCopyWith<_LoginRequestModel> get copyWith => __$LoginRequestModelCopyWithImpl<_LoginRequestModel>(this, _$identity);
@override
Map<String, dynamic> toJson() {
return _$LoginRequestModelToJson(this, );
}
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is _LoginRequestModel&&(identical(other.username, username) || other.username == username)&&(identical(other.password, password) || other.password == password)&&(identical(other.captchaCode, captchaCode) || other.captchaCode == captchaCode)&&(identical(other.captchaKey, captchaKey) || other.captchaKey == captchaKey));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType,username,password,captchaCode,captchaKey);
@override
String toString() {
return 'LoginRequestModel(username: $username, password: $password, captchaCode: $captchaCode, captchaKey: $captchaKey)';
}
}
/// @nodoc
abstract mixin class _$LoginRequestModelCopyWith<$Res> implements $LoginRequestModelCopyWith<$Res> {
factory _$LoginRequestModelCopyWith(_LoginRequestModel value, $Res Function(_LoginRequestModel) _then) = __$LoginRequestModelCopyWithImpl;
@override @useResult
$Res call({
String? username, String? password, String? captchaCode, String? captchaKey
});
}
/// @nodoc
class __$LoginRequestModelCopyWithImpl<$Res>
implements _$LoginRequestModelCopyWith<$Res> {
__$LoginRequestModelCopyWithImpl(this._self, this._then);
final _LoginRequestModel _self;
final $Res Function(_LoginRequestModel) _then;
/// Create a copy of LoginRequestModel
/// with the given fields replaced by the non-null parameter values.
@override @pragma('vm:prefer-inline') $Res call({Object? username = freezed,Object? password = freezed,Object? captchaCode = freezed,Object? captchaKey = freezed,}) {
return _then(_LoginRequestModel(
username: freezed == username ? _self.username : username // ignore: cast_nullable_to_non_nullable
as String?,password: freezed == password ? _self.password : password // ignore: cast_nullable_to_non_nullable
as String?,captchaCode: freezed == captchaCode ? _self.captchaCode : captchaCode // ignore: cast_nullable_to_non_nullable
as String?,captchaKey: freezed == captchaKey ? _self.captchaKey : captchaKey // ignore: cast_nullable_to_non_nullable
as String?,
));
}
}
// dart format on

View File

@@ -0,0 +1,23 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'login_request_model.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
_LoginRequestModel _$LoginRequestModelFromJson(Map<String, dynamic> json) =>
_LoginRequestModel(
username: json['username'] as String?,
password: json['password'] as String?,
captchaCode: json['captchaCode'] as String?,
captchaKey: json['captchaKey'] as String?,
);
Map<String, dynamic> _$LoginRequestModelToJson(_LoginRequestModel instance) =>
<String, dynamic>{
'username': instance.username,
'password': instance.password,
'captchaCode': instance.captchaCode,
'captchaKey': instance.captchaKey,
};

View File

@@ -0,0 +1,20 @@
import 'package:freezed_annotation/freezed_annotation.dart';
part 'auth_response_model.freezed.dart';
part 'auth_response_model.g.dart';
@freezed
abstract class AuthResponseModel with _$AuthResponseModel {
const factory AuthResponseModel({
String? refresh,
String? access,
@JsonKey(name: 'otp_status') bool? otpStatus,
}) = _AuthResponseModel;
factory AuthResponseModel.fromJson(Map<String, dynamic> json) =>
_$AuthResponseModelFromJson(json);
}

View File

@@ -0,0 +1,17 @@
import 'package:freezed_annotation/freezed_annotation.dart';
part 'captcha_response_model.freezed.dart';
part 'captcha_response_model.g.dart';
@freezed
abstract class CaptchaResponseModel with _$CaptchaResponseModel {
const factory CaptchaResponseModel({
String? captchaKey,
String? captchaImage,
String? imageType,
String? imageDecode,
}) = _CaptchaResponseModel;
factory CaptchaResponseModel.fromJson(Map<String, dynamic> json) =>
_$CaptchaResponseModelFromJson(json);
}

View File

@@ -0,0 +1,23 @@
import '../models/response/auth/auth_response_model.dart';
import '../models/response/captcha/captcha_response_model.dart';
abstract class AuthRepository {
Future<AuthResponseModel?> login({
required Map<String, dynamic> authRequest,
});
Future<CaptchaResponseModel?> captcha();
Future<void> logout();
Future<bool> hasAuthenticated();
Future<AuthResponseModel?> loginWithRefreshToken({
required Map<String, dynamic> authRequest,
});
}

View File

@@ -0,0 +1,100 @@
import 'package:rasadyar_core/core.dart';
import '../models/response/auth/auth_response_model.dart';
import '../models/response/captcha/captcha_response_model.dart';
import 'auth_repository.dart';
class AuthRepositoryImpl implements AuthRepository {
final DioRemote _httpClient;
final String _BASE_URL = 'auth/api/v1/';
AuthRepositoryImpl(this._httpClient);
@override
Future<AuthResponseModel?> login({
required Map<String, dynamic> authRequest,
}) async {
final response = await safeCall<DioResponse<AuthResponseModel>>(
call:
() async => await _httpClient.post<AuthResponseModel>(
'$_BASE_URL/login/',
data: authRequest,
headers: {'Content-Type': 'application/json'},
),
onSuccess: (response) {
iLog(response);
},
onError: (error, trace) {
throw Exception('Error during sign in: $error');
},
);
return response?.data;
}
@override
Future<CaptchaResponseModel?> captcha() async {
final response = await safeCall<DioResponse<CaptchaResponseModel>>(
call:
() async => await _httpClient.post<CaptchaResponseModel>(
'$_BASE_URL/login/',
headers: {'Content-Type': 'application/json'},
),
onSuccess: (response) {
iLog(response);
},
onError: (error, trace) {
throw Exception('Error during sign in: $error');
},
);
return response?.data;
}
@override
Future<AuthResponseModel?> loginWithRefreshToken({
required Map<String, dynamic> authRequest,
}) async {
final response = await safeCall<DioResponse<AuthResponseModel>>(
call:
() async => await _httpClient.post<AuthResponseModel>(
'$_BASE_URL/login/',
data: authRequest,
headers: {'Content-Type': 'application/json'},
),
onSuccess: (response) {
iLog(response);
},
onError: (error, trace) {
throw Exception('Error during sign in: $error');
},
);
return response?.data;
}
@override
Future<void> logout() {
// TODO: implement logout
throw UnimplementedError();
}
@override
Future<bool> hasAuthenticated() async {
final response = await safeCall<DioResponse<bool>>(
call:
() async => await _httpClient.get<bool>(
'$_BASE_URL/login/',
headers: {'Content-Type': 'application/json'},
),
onSuccess: (response) {
iLog(response);
},
onError: (error, trace) {
throw Exception('Error during sign in: $error');
},
);
return response?.data ?? false;
}
}

View File

@@ -0,0 +1,16 @@
import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';
import '../../presentation/routes/pages.dart';
class AuthMiddleware extends GetMiddleware{
@override
RouteSettings? redirect(String? route) {
if(route == AuthPaths.auth) {
return const RouteSettings(name: AuthPaths.moduleList);
}
return super.redirect(route);
}
}

View File

@@ -0,0 +1,9 @@
import 'package:rasadyar_core/core.dart';
class AuthService extends GetxService{
Future<void> initService() async {
}
}

View File

@@ -0,0 +1,59 @@
import 'dart:convert';
import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_core/injection/di.dart';
class TokenStorageService extends GetxService {
static const String _boxName = 'secureBox';
static const String _accessTokenKey = 'accessToken';
static const String _refreshTokenKey = 'refreshToken';
final FlutterSecureStorage _secureStorage = FlutterSecureStorage();
final HiveLocalStorage _localStorage = diCore.get<HiveLocalStorage>();
Future<void> init() async {
final String? encryptedKey = await _secureStorage.read(key: 'hive_enc_key');
final encryptionKey =
encryptedKey != null
? base64Url.decode(encryptedKey)
: Hive.generateSecureKey();
if (encryptedKey == null) {
await _secureStorage.write(
key: 'hive_enc_key',
value: base64UrlEncode(encryptionKey),
);
}
await Hive.initFlutter();
await Hive.openBox(
_boxName,
encryptionCipher: HiveAesCipher(encryptionKey),
);
}
Future<void> saveAccessToken(String token) async {
final box = Hive.box(_boxName);
await box.put(_accessTokenKey, token);
}
Future<void> saveRefreshToken(String token) async {
final box = Hive.box(_boxName);
await box.put(_refreshTokenKey, token);
}
Future<String?> getAccessToken() async {
final box = Hive.box(_boxName);
return box.get(_accessTokenKey);
}
Future<String?> getRefreshToken() async {
final box = Hive.box(_boxName);
return box.get(_refreshTokenKey);
}
Future<void> deleteTokens() async {
final box = Hive.box(_boxName);
await box.delete(_accessTokenKey);
await box.delete(_refreshTokenKey);
}
}