Refactor HTTPInterceptor and add CustomSnackBar helper

Refactor HTTPInterceptor to handle error responses and add a CustomSnackBar
helper to display snack bars. This will improve error handling and user
feedback in the application.
This commit is contained in:
Mohammad Salameh
2024-04-15 12:02:34 +03:00
parent 590c70a7d8
commit cfc395e210
10 changed files with 287 additions and 188 deletions

View File

@ -1,13 +1,13 @@
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/auth/model/login_with_email_model.dart';
import 'package:syncrow_app/features/auth/model/token.dart';
import 'package:syncrow_app/features/auth/model/user_model.dart';
import 'package:syncrow_app/navigation/navigation_service.dart';
import 'package:syncrow_app/navigation/routing_constants.dart';
import 'package:syncrow_app/services/api/authentication_api.dart';
import 'package:syncrow_app/services/api/network_exception.dart';
@ -43,7 +43,7 @@ class AuthCubit extends Cubit<AuthState> {
static Token token = Token.emptyConstructor();
login() async {
emit(AuthLoading());
emit(AuthLoginLoading());
try {
token = await AuthenticationAPI.loginWithEmail(
model: LoginWithEmailModel(
@ -60,24 +60,27 @@ class AuthCubit extends Cubit<AuthState> {
key: UserModel.userUuidKey,
value: Token.decodeToken(token.accessToken)['uuid'].toString());
user = UserModel.fromToken(token);
emit(AuthSuccess());
emailController.clear();
passwordController.clear();
emit(AuthLoginSuccess());
} else {
emit(AuthError('Something went wrong'));
emit(AuthLoginError(message: 'Something went wrong'));
}
} on DioException catch (e) {
emit(AuthError(ServerFailure.fromDioError(e).toString()));
} on ServerFailure catch (failure) {
emit(AuthError(message: failure.errMessage));
}
}
logout() async {
emit(AuthLoading());
emit(AuthLogoutLoading());
try {
FlutterSecureStorage storage = const FlutterSecureStorage();
await storage.delete(key: Token.loginAccessTokenKey);
HomeCubit.clear();
emit(AuthLoggedOut());
} on DioException catch (e) {
emit(AuthError(ServerFailure.fromDioError(e).errMessage));
NavigationService.navigatorKey.currentState!
.popAndPushNamed(Routes.authLogin);
emit(AuthLogoutSuccess());
} on ServerFailure catch (failure) {
emit(AuthError(message: failure.errMessage));
}
}
@ -87,7 +90,7 @@ class AuthCubit extends Cubit<AuthState> {
await const FlutterSecureStorage().read(key: Token.loginAccessTokenKey);
if (value == null) {
emit(AuthTokenError("Token not found"));
emit(AuthTokenError(message: "Token not found"));
return;
}
@ -100,10 +103,17 @@ class AuthCubit extends Cubit<AuthState> {
if (currentTime < exp) {
emit(AuthTokenSuccess());
} else {
emit(AuthTokenError("Token expired"));
emit(AuthTokenError(message: "Token expired"));
}
} else {
emit(AuthTokenError("Something went wrong"));
emit(AuthTokenError(message: "Something went wrong"));
}
}
static void logUserOut() async {
user = null;
token = Token.emptyConstructor();
FlutterSecureStorage storage = const FlutterSecureStorage();
await storage.delete(key: Token.loginAccessTokenKey);
}
}