fix : don't redirect to login
This commit is contained in:
@@ -2,8 +2,6 @@ import 'package:rasadyar_auth/auth.dart';
|
|||||||
import 'package:rasadyar_auth/data/repositories/auth_repository_imp.dart';
|
import 'package:rasadyar_auth/data/repositories/auth_repository_imp.dart';
|
||||||
import 'package:rasadyar_core/core.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>({
|
Future<void> safeCall<T>({
|
||||||
required AppAsyncCallback<T> call,
|
required AppAsyncCallback<T> call,
|
||||||
Function(T result)? onSuccess,
|
Function(T result)? onSuccess,
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'package:rasadyar_auth/presentation/routes/pages.dart';
|
||||||
|
|
||||||
import '../../core.dart';
|
import '../../core.dart';
|
||||||
|
|
||||||
typedef RefreshTokenCallback = Future<String?> Function();
|
typedef RefreshTokenCallback = Future<String?> Function();
|
||||||
@@ -10,7 +12,7 @@ class AppInterceptor extends Interceptor {
|
|||||||
@override
|
@override
|
||||||
Future<void> onError(DioException err, ErrorInterceptorHandler handler) async {
|
Future<void> onError(DioException err, ErrorInterceptorHandler handler) async {
|
||||||
if (err.response?.statusCode == 401 && !ApiHandler.isRefreshing) {
|
if (err.response?.statusCode == 401 && !ApiHandler.isRefreshing) {
|
||||||
// اول بقیه درخواستها رو کنسل کن
|
|
||||||
ApiHandler.cancelAllRequests("Token expired - refreshing");
|
ApiHandler.cancelAllRequests("Token expired - refreshing");
|
||||||
|
|
||||||
ApiHandler.isRefreshing = true;
|
ApiHandler.isRefreshing = true;
|
||||||
@@ -19,7 +21,7 @@ class AppInterceptor extends Interceptor {
|
|||||||
final newToken = await refreshTokenCallback();
|
final newToken = await refreshTokenCallback();
|
||||||
if (newToken == null) throw Exception("Refresh failed");
|
if (newToken == null) throw Exception("Refresh failed");
|
||||||
|
|
||||||
// تولید CancelToken جدید برای درخواستهای بعدی
|
|
||||||
ApiHandler.reset();
|
ApiHandler.reset();
|
||||||
|
|
||||||
final opts = err.requestOptions;
|
final opts = err.requestOptions;
|
||||||
@@ -34,8 +36,9 @@ class AppInterceptor extends Interceptor {
|
|||||||
if (!ApiHandler.isRedirecting) {
|
if (!ApiHandler.isRedirecting) {
|
||||||
ApiHandler.isRedirecting = true;
|
ApiHandler.isRedirecting = true;
|
||||||
ApiHandler.cancelAllRequests("Cancel All Requests - Unauthorized");
|
ApiHandler.cancelAllRequests("Cancel All Requests - Unauthorized");
|
||||||
// TODO: Navigate to login
|
if (Get.currentRoute != '/Auth') {
|
||||||
Get.offAllNamed('/login');
|
Get.offAllNamed('/Auth');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
handler.reject(err);
|
handler.reject(err);
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -11,12 +11,11 @@ class ApiHandler {
|
|||||||
globalCancelToken = CancelToken();
|
globalCancelToken = CancelToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
// متد جدید برای کنسل کردن همه درخواستها
|
|
||||||
static void cancelAllRequests(String reason) {
|
static void cancelAllRequests(String reason) {
|
||||||
if (!globalCancelToken.isCancelled) {
|
if (!globalCancelToken.isCancelled) {
|
||||||
globalCancelToken.cancel(reason);
|
globalCancelToken.cancel(reason);
|
||||||
}
|
}
|
||||||
// CancelToken جدید بساز برای درخواستهای بعدی
|
|
||||||
globalCancelToken = CancelToken();
|
globalCancelToken = CancelToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user