62 lines
1.9 KiB
Dart
62 lines
1.9 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:rasadyar_auth/auth.dart';
|
|
import 'package:rasadyar_auth/data/repositories/auth_repository_imp.dart';
|
|
import 'package:rasadyar_core/core.dart';
|
|
|
|
import '../models/response/auth/auth_response_model.dart';
|
|
import '../services/token_storage_service.dart';
|
|
|
|
Future<void> safeCall<T>({
|
|
required AppAsyncCallback<T> call,
|
|
Function(T result)? onSuccess,
|
|
ErrorCallback? onError,
|
|
VoidCallback? onComplete,
|
|
bool showLoading = false,
|
|
bool showError = false,
|
|
bool showSuccess = false,
|
|
bool showToast = false,
|
|
bool showSnackBar = false,
|
|
Function()? onShowLoading,
|
|
Function()? onHideLoading,
|
|
Function()? onShowSuccessMessage,
|
|
Function()? onShowErrorMessage,
|
|
}) {
|
|
final authRepository = diAuth.get<AuthRepositoryImpl>();
|
|
TokenStorageService tokenStorageService = Get.find<TokenStorageService>();
|
|
|
|
return gSafeCall(
|
|
call: call,
|
|
onSuccess: onSuccess,
|
|
onError: onError,
|
|
onComplete: onComplete,
|
|
showLoading: showLoading,
|
|
showError: showError,
|
|
showSuccess: showSuccess,
|
|
showToast: showToast,
|
|
showSnackBar: showSnackBar,
|
|
onShowLoading: onShowLoading,
|
|
onHideLoading: onHideLoading,
|
|
onShowSuccessMessage: onShowSuccessMessage,
|
|
onShowErrorMessage: onShowErrorMessage,
|
|
retryOnAuthError: true,
|
|
onTokenRefresh: () {
|
|
var token = tokenStorageService.refreshToken.value;
|
|
authRepository
|
|
.loginWithRefreshToken(authRequest: {"refresh_token": token})
|
|
.then((value) async {
|
|
if (value is AuthResponseModel) {
|
|
await tokenStorageService.saveAccessToken(value.access!);
|
|
} else {
|
|
throw Exception("Failed to refresh token");
|
|
}
|
|
})
|
|
.catchError((error) {
|
|
if (kDebugMode) {
|
|
print('Error during token refresh: $error');
|
|
}
|
|
throw error;
|
|
});
|
|
},
|
|
);
|
|
}
|