feat : change app inspector and exception handling
This commit is contained in:
@@ -4,15 +4,21 @@ import 'package:rasadyar_core/core.dart';
|
||||
import '../di/auth_di.dart';
|
||||
import 'constant.dart';
|
||||
|
||||
/*
|
||||
class DioRemoteManager {
|
||||
DioRemote? _currentClient;
|
||||
ApiEnvironment? _currentEnv;
|
||||
|
||||
Future<DioRemote> setEnvironment([
|
||||
ApiEnvironment env = ApiEnvironment.dam,
|
||||
]) async {
|
||||
Future<DioRemote> setEnvironment([ApiEnvironment env = ApiEnvironment.dam]) async {
|
||||
if (_currentEnv != env) {
|
||||
_currentClient = DioRemote(baseUrl: env.baseUrl);
|
||||
_currentClient = DioRemote(
|
||||
baseUrl: env.baseUrl,
|
||||
interceptors: AppInterceptor(
|
||||
refreshTokenCallback: () async{
|
||||
return null;
|
||||
},
|
||||
),
|
||||
);
|
||||
await _currentClient?.init();
|
||||
_currentEnv = env;
|
||||
}
|
||||
@@ -39,7 +45,6 @@ Future<void> switchAuthEnvironment(ApiEnvironment env) async {
|
||||
await diAuth.unregister<AuthRepositoryImpl>();
|
||||
}
|
||||
|
||||
diAuth.registerLazySingleton<AuthRepositoryImpl>(
|
||||
() => AuthRepositoryImpl(dioRemote),
|
||||
);
|
||||
diAuth.registerLazySingleton<AuthRepositoryImpl>(() => AuthRepositoryImpl(dioRemote));
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -9,7 +9,7 @@ import '../common/dio_manager.dart';
|
||||
GetIt diAuth = GetIt.instance;
|
||||
|
||||
Future<void> setupAuthDI() async {
|
||||
diAuth.registerLazySingleton(() => DioRemoteManager());
|
||||
|
||||
diAuth.registerLazySingleton<AppInterceptor>(
|
||||
() => AppInterceptor(
|
||||
refreshTokenCallback: () async {
|
||||
@@ -19,7 +19,9 @@ Future<void> setupAuthDI() async {
|
||||
final refreshToken = tokenService.refreshToken.value;
|
||||
if (refreshToken == null) return null;
|
||||
|
||||
final result = await authRepo.loginWithRefreshToken(authRequest: {"refresh_token": refreshToken});
|
||||
final result = await authRepo.loginWithRefreshToken(
|
||||
authRequest: {"refresh_token": refreshToken},
|
||||
);
|
||||
|
||||
if (result is AuthResponseModel) {
|
||||
await tokenService.saveAccessToken(result.access!);
|
||||
@@ -27,10 +29,18 @@ Future<void> setupAuthDI() async {
|
||||
}
|
||||
return null;
|
||||
},
|
||||
saveTokenCallback: (String newToken) async {
|
||||
//
|
||||
},
|
||||
clearTokenCallback: () async {
|
||||
//await tokenService.clearTokens(); // حذف همه توکنها
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
diAuth.registerLazySingleton<DioRemote>(() => DioRemote(interceptors: [diAuth.get<AppInterceptor>()]));
|
||||
diAuth.registerLazySingleton<DioRemote>(
|
||||
() => DioRemote(interceptors: diAuth.get<AppInterceptor>()),
|
||||
);
|
||||
|
||||
final dioRemote = diAuth.get<DioRemote>();
|
||||
await dioRemote.init();
|
||||
@@ -40,10 +50,13 @@ Future<void> setupAuthDI() async {
|
||||
|
||||
Future<void> newSetupAuthDI(String newUrl) async {
|
||||
diAuth.registerLazySingleton<DioRemote>(
|
||||
() => DioRemote(baseUrl: newUrl, interceptors: [diAuth.get<AppInterceptor>()]),
|
||||
() => DioRemote(baseUrl: newUrl, interceptors: diAuth.get<AppInterceptor>()),
|
||||
instanceName: 'newRemote',
|
||||
);
|
||||
final dioRemote = diAuth.get<DioRemote>(instanceName: 'newRemote');
|
||||
await dioRemote.init();
|
||||
diAuth.registerSingleton<AuthRepositoryImpl>(AuthRepositoryImpl(dioRemote), instanceName: 'newUrl');
|
||||
diAuth.registerSingleton<AuthRepositoryImpl>(
|
||||
AuthRepositoryImpl(dioRemote),
|
||||
instanceName: 'newUrl',
|
||||
);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,8 @@ import 'package:rasadyar_auth/hive_registrar.g.dart';
|
||||
import 'package:rasadyar_core/core.dart';
|
||||
|
||||
class TokenStorageService extends GetxService {
|
||||
static const String _boxName = 'secureBox';
|
||||
static const String _tokenBoxName = 'TokenBox';
|
||||
static const String _appBoxName = 'AppBox';
|
||||
static const String _accessTokenKey = 'accessToken';
|
||||
static const String _refreshTokenKey = 'refreshToken';
|
||||
static const String _baseUrlKey = 'baseUrl';
|
||||
@@ -17,7 +18,7 @@ class TokenStorageService extends GetxService {
|
||||
|
||||
RxnString accessToken = RxnString();
|
||||
RxnString refreshToken = RxnString();
|
||||
RxnString baseurl= RxnString();
|
||||
RxnString baseurl = RxnString();
|
||||
Rxn<Module> appModule = Rxn(null);
|
||||
|
||||
Future<void> init() async {
|
||||
@@ -25,54 +26,61 @@ class TokenStorageService extends GetxService {
|
||||
Hive.registerAdapters();
|
||||
|
||||
final String? encryptedKey = await _secureStorage.read(key: 'hive_enc_key');
|
||||
final encryptionKey = encryptedKey != null ? base64Url.decode(encryptedKey) : Hive.generateSecureKey();
|
||||
final encryptionKey = encryptedKey != null
|
||||
? base64Url.decode(encryptedKey)
|
||||
: Hive.generateSecureKey();
|
||||
|
||||
if (encryptedKey == null) {
|
||||
await _secureStorage.write(key: 'hive_enc_key', value: base64UrlEncode(encryptionKey));
|
||||
}
|
||||
|
||||
await _localStorage.init();
|
||||
await _localStorage.openBox(_boxName, encryptionCipher: HiveAesCipher(encryptionKey));
|
||||
await _localStorage.openBox(_tokenBoxName, encryptionCipher: HiveAesCipher(encryptionKey));
|
||||
await _localStorage.openBox(_appBoxName);
|
||||
|
||||
accessToken.value = _localStorage.read<String?>(boxName: _boxName, key: _accessTokenKey);
|
||||
refreshToken.value = _localStorage.read<String?>(boxName: _boxName, key: _refreshTokenKey);
|
||||
appModule.value = _localStorage.read<Module?>(boxName: _boxName, key: _moduleKey);
|
||||
baseurl.value = _localStorage.read<String?>(boxName: _boxName, key: _baseUrlKey);
|
||||
accessToken.value = _localStorage.read<String?>(boxName: _tokenBoxName, key: _accessTokenKey);
|
||||
refreshToken.value = _localStorage.read<String?>(boxName: _tokenBoxName, key: _refreshTokenKey);
|
||||
appModule.value = _localStorage.read<Module?>(boxName: _appBoxName, key: _moduleKey);
|
||||
baseurl.value = _localStorage.read<String?>(boxName: _appBoxName, key: _baseUrlKey);
|
||||
}
|
||||
|
||||
Future<void> saveAccessToken(String token) async {
|
||||
await _localStorage.save(boxName: _boxName, key: _accessTokenKey, value: token);
|
||||
await _localStorage.save(boxName: _tokenBoxName, key: _accessTokenKey, value: token);
|
||||
accessToken.value = token;
|
||||
accessToken.refresh();
|
||||
}
|
||||
|
||||
Future<void> saveRefreshToken(String token) async {
|
||||
await _localStorage.save(boxName: _boxName, key: _refreshTokenKey, value: token);
|
||||
await _localStorage.save(boxName: _tokenBoxName, key: _refreshTokenKey, value: token);
|
||||
refreshToken.value = token;
|
||||
refreshToken.refresh();
|
||||
}
|
||||
|
||||
Future<void> saveModule(Module input) async {
|
||||
await _localStorage.save(boxName: _boxName, key: _moduleKey, value: input);
|
||||
await _localStorage.save(boxName: _tokenBoxName, key: _moduleKey, value: input);
|
||||
appModule.value = input;
|
||||
appModule.refresh();
|
||||
}
|
||||
|
||||
Future<void> deleteTokens() async {
|
||||
await _localStorage.clear(_boxName);
|
||||
await _localStorage.clear(_tokenBoxName);
|
||||
accessToken.value = null;
|
||||
refreshToken.value = null;
|
||||
}
|
||||
|
||||
|
||||
Future<void> saveBaseUrl(String url) async {
|
||||
await _localStorage.save(boxName: _boxName, key: _baseUrlKey, value: url);
|
||||
await _localStorage.save(boxName: _appBoxName, key: _baseUrlKey, value: url);
|
||||
baseurl.value = url;
|
||||
baseurl.refresh();
|
||||
}
|
||||
|
||||
void getBaseUrl() {
|
||||
var url = _localStorage.read(boxName: _appBoxName, key: _baseUrlKey);
|
||||
baseurl.value = url;
|
||||
baseurl.refresh();
|
||||
}
|
||||
|
||||
Future<void> saveApiKey(String key) async {
|
||||
await _localStorage.save(boxName: _boxName, key: _apiKey, value: key);
|
||||
|
||||
await _localStorage.save(boxName: _tokenBoxName, key: _apiKey, value: key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'package:rasadyar_core/core.dart';
|
||||
|
||||
Future<void> safeCall<T>({
|
||||
Future<T?> safeCall<T>({
|
||||
required AppAsyncCallback<T> call,
|
||||
Function(T result)? onSuccess,
|
||||
ErrorCallback? onError,
|
||||
@@ -12,10 +12,8 @@ Future<void> safeCall<T>({
|
||||
bool showSnackBar = false,
|
||||
Function()? onShowLoading,
|
||||
Function()? onHideLoading,
|
||||
Function()? onShowSuccessMessage,
|
||||
Function()? onShowErrorMessage,
|
||||
}) {
|
||||
return gSafeCall(
|
||||
return gSafeCall<T>(
|
||||
call: call,
|
||||
onSuccess: onSuccess,
|
||||
onError: onError,
|
||||
@@ -23,12 +21,7 @@ Future<void> safeCall<T>({
|
||||
showLoading: showLoading,
|
||||
showError: showError,
|
||||
showSuccess: showSuccess,
|
||||
showToast: showToast,
|
||||
showSnackBar: showSnackBar,
|
||||
onShowLoading: onShowLoading,
|
||||
onHideLoading: onHideLoading,
|
||||
onShowSuccessMessage: onShowSuccessMessage,
|
||||
onShowErrorMessage: onShowErrorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user