diff --git a/lib/presentation/app/bloc/app_bloc.dart b/lib/presentation/app/bloc/app_bloc.dart index 0d0cde97..24b50b84 100644 --- a/lib/presentation/app/bloc/app_bloc.dart +++ b/lib/presentation/app/bloc/app_bloc.dart @@ -1,5 +1,4 @@ import 'dart:async'; - import 'package:appetizer/data/constants/constants.dart'; import 'package:appetizer/data/services/local/local_storage_service.dart'; import 'package:appetizer/domain/models/user/user.dart'; diff --git a/lib/utils/interceptors/auth_interceptor.dart b/lib/utils/interceptors/auth_interceptor.dart index 8df4423c..d44926b3 100644 --- a/lib/utils/interceptors/auth_interceptor.dart +++ b/lib/utils/interceptors/auth_interceptor.dart @@ -1,12 +1,18 @@ import 'dart:async'; - +import 'package:appetizer/data/constants/constants.dart'; +import 'package:appetizer/data/services/local/local_storage_service.dart'; +import 'package:appetizer/presentation/app/bloc/app_bloc.dart'; +import 'package:appetizer/utils/app_extensions/app_extensions.dart'; import 'package:dio/dio.dart'; -import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; class AuthInterceptor extends Interceptor { ValueGetter>? getToken; - AuthInterceptor({this.getToken}); + AuthInterceptor({ + this.getToken, + }); @override void onRequest( @@ -22,4 +28,22 @@ class AuthInterceptor extends Interceptor { return super.onRequest(options, handler); } + + @override + void onError(DioException err, ErrorInterceptorHandler handler) { + if (err.response?.statusCode == 401) { + LocalStorageService.setValue(key: AppConstants.LOGGED_IN, value: false); + LocalStorageService.setValue(key: AppConstants.AUTH_TOKEN, value: null); + if (BaseApp.currentContext != null) { + const snackBar = + SnackBar(content: Text('User is inactive or unauthorized!')); + ScaffoldMessenger.of(BaseApp.currentContext!).showSnackBar(snackBar); + } + BaseApp.currentContext + ?.read() + .add(const NavigateToLoginScreen()); + } else { + handler.next(err); + } + } }