From 70e9b8371aae4b862b3c8a8eae308f28fbc3e1da Mon Sep 17 00:00:00 2001 From: NanoNish Date: Sun, 1 Oct 2023 01:14:22 +0530 Subject: [PATCH 1/2] feat: logout on 401 (unauthorized) response --- lib/presentation/app/bloc/app_bloc.dart | 1 - lib/utils/interceptors/auth_interceptor.dart | 25 +++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) 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..1dc64bb1 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,17 @@ 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); + BaseApp.currentContext + ?.read() + .add(const NavigateToLoginScreen()); + } else { + handler.next(err); + } + } } From dddd0594b9b4d9e4e647fc02084861503a4b7b2c Mon Sep 17 00:00:00 2001 From: NanoNish Date: Sun, 1 Oct 2023 01:15:45 +0530 Subject: [PATCH 2/2] feat: show snackbar on logout at 401 --- lib/utils/interceptors/auth_interceptor.dart | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/utils/interceptors/auth_interceptor.dart b/lib/utils/interceptors/auth_interceptor.dart index 1dc64bb1..d44926b3 100644 --- a/lib/utils/interceptors/auth_interceptor.dart +++ b/lib/utils/interceptors/auth_interceptor.dart @@ -34,6 +34,11 @@ class AuthInterceptor extends Interceptor { 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());