Aidra Driver 1.3.5+68
Aidra Driver - Your path to green energy
Loading...
Searching...
No Matches
auth_interceptor.dart
Go to the documentation of this file.
1import 'package:dio/dio.dart';
2import 'package:flutter_bloc/flutter_bloc.dart';
3import 'package:go_router/go_router.dart';
4
5import '../../features/auth/presentation/bloc/authentication_bloc/authentication_bloc.dart';
6import '../../features/auth/presentation/widgets/fast_sign_in_bottom_sheet.dart';
7import '../constants/strings.dart';
8import '../router/router.dart';
9import '../router/routes.dart';
10import '../services/auth_service.dart';
11import '../services/fast_sign_in_service.dart';
12import '../services/secure_storage_service.dart';
13import '../ui/theme/color_palette.dart';
14import '../ui/widgets/custom_snackbar.dart';
15import 'api_endpoints.dart';
16
18 final Dio dio;
19
20 AuthInterceptor({required this.dio});
21
25
26 @override
28 RequestOptions options,
29 RequestInterceptorHandler handler,
30 ) async {
31 final token = await _tokenSecureStorage.get();
32 if (token != null) {
33 options.headers['Authorization'] = 'Bearer $token';
34 }
35
36 return handler.next(options);
37 }
38
39 @override
41 Response response,
42 ResponseInterceptorHandler handler,
43 ) async {
44 final currentContext = rootNavigatorKey.currentContext;
45 final currentRoute = currentContext != null
46 ? GoRouter.of(currentContext).state.matchedLocation
47 : '';
48 final isLoginRoute = currentRoute == Routes.signInScreen.route;
49 final isSplashRoute = currentRoute == Routes.splashScreen.route;
50
51 final requestUrl = response.requestOptions.uri.toString();
52 final isGeoLocationUrl = requestUrl == ApiEndpoints.sendGeoLocation;
53 final isTokenValid = await _authService.isTokenValid();
54 if ((response.statusCode == 401 || !isTokenValid) &&
55 !isGeoLocationUrl &&
56 !isLoginRoute &&
57 !isSplashRoute) {
59 }
60 return handler.next(response);
61 }
62
63 Future<void> _handleTokenExpiration() async {
64 final context = rootNavigatorKey.currentContext;
65 if (context == null) return;
66 final authBloc = BlocProvider.of<AuthenticationBloc>(context);
67 authBloc.add(LoadEmailEvent());
68
69 String? email;
70 await for (final state in authBloc.stream) {
71 if (state is EmailLoadedState) {
72 email = state.email;
73 break;
74 }
75 }
76
77 // Set fast sign-in as active before showing the bottom sheet
78 _fastSignInService.setActive(true);
79
80 FastSignInBottomSheet.show(
81 context,
82 email: email,
83 onSuccess: () {
84 _fastSignInService.setActive(false);
85 authBloc.add(LoadSessionEvent());
87 context,
89 "Successfully logged in",
90 );
91 },
92 onCancel: () {
93 _fastSignInService.setActive(false);
95 context,
97 "Login failed",
98 );
99 },
100 ).then((result) {
101 // Ensure the flag is cleared if the bottom sheet is dismissed
102 if (result == null || result == false) {
103 _fastSignInService.setActive(false);
104 }
105 });
106 }
107}
static const lightGreen
static const red
static ScaffoldFeatureController< SnackBar, SnackBarClosedReason > display(final BuildContext context, final Color color, final String message,)
AuthInterceptor({required this.dio})
final FastSignInService _fastSignInService
override void onResponse(Response response, ResponseInterceptorHandler handler,) async
final AuthService _authService
Future< void > _handleTokenExpiration() async
override void onRequest(RequestOptions options, RequestInterceptorHandler handler,) async
static const String tokenKey
Definition strings.dart:8
class Partner String
final rootNavigatorKey
Definition router.dart:10
Routes
Definition routes.dart:30