diff --git a/lib/features/auth/bloc/auth_cubit.dart b/lib/features/auth/bloc/auth_cubit.dart index ee53a56..7e4709b 100644 --- a/lib/features/auth/bloc/auth_cubit.dart +++ b/lib/features/auth/bloc/auth_cubit.dart @@ -6,6 +6,7 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/auth/model/token.dart'; import 'package:syncrow_app/features/auth/model/user_model.dart'; import 'package:syncrow_app/services/api/authentication_api.dart'; +import 'package:syncrow_app/services/api/network_exception.dart'; import 'package:syncrow_app/utils/helpers/decode_base64.dart'; part 'auth_state.dart'; @@ -47,17 +48,23 @@ class AuthCubit extends Cubit { password: passwordController.text, ); - emit(AuthSuccess()); + if (token.accessTokenIsNotEmpty) { + final parts = token.accessToken.split('.'); + if (parts.length != 3) { + throw Exception('invalid access token'); + } + final payload = decodeBase64(parts[1]); + final payloadMap = json.decode(payload); //Map dictionary + user = UserModel.fromToken(payloadMap); + emit(AuthSuccess()); + } else { + emit(AuthError('Something went wrong')); + } } catch (e) { + if (e is ServerFailure) { + emit(AuthError(e.errMessage)); + } emit(AuthError(e.toString())); } - - final parts = token.accessToken.split('.'); - if (parts.length != 3) { - throw Exception('invalid access token'); - } - final payload = decodeBase64(parts[1]); - final payloadMap = json.decode(payload); //Map dictionary - user = UserModel.fromToken(payloadMap); } } diff --git a/lib/features/auth/model/token.dart b/lib/features/auth/model/token.dart index 95f45eb..d63fdcd 100644 --- a/lib/features/auth/model/token.dart +++ b/lib/features/auth/model/token.dart @@ -11,6 +11,8 @@ class Token { : accessToken = '', refreshToken = ''; + bool get accessTokenIsNotEmpty => accessToken.isNotEmpty; + Token( this.accessToken, this.refreshToken, diff --git a/lib/services/api/authentication_api.dart b/lib/services/api/authentication_api.dart index 4f8fea0..5b8b661 100644 --- a/lib/services/api/authentication_api.dart +++ b/lib/services/api/authentication_api.dart @@ -35,106 +35,4 @@ class AuthenticationAPI { debugPrint("response: $response"); return response; } - -// static Future updateUserInfo( -// Map data) async { -// final response = await HTTPService().postRequest( -// path: APIConstants.updateUserInfo, -// body: data, -// expectedResponseModel: (json) { -// SuccessResponse token = SuccessResponse.fromJson(json); -// return token; -// }); -// return response; -// } -// -// static Future loginWithPhone( -// {required LoginWithPhone data, String? recaptchaToken}) async { -// final response = await HTTPService().postRequest( -// path: APIConstants.loginWithChannel, -// body: data.toJson(), -// options: Options(headers: { -// 'captcha-token': recaptchaToken, -// 'captcha-site-key': Platform.isAndroid -// ? dotenv.env['RECAPTCH_ANDROID_SITE_KEY'] ?? '' -// : dotenv.env['RECAPTCH_IOS_SITE_KEY'] ?? '' -// }), -// expectedResponseModel: (json) { -// LoginWithPhoneResponse result = LoginWithPhoneResponse.fromJson(json); -// return result; -// }); -// return response; -// } -// -// static Future> countryCodes() async { -// final response = await HTTPService().postRequest( -// path: APIConstants.getCountyCode, -// expectedResponseModel: (json) { -// List result = json.toList(); -// return result; -// }); -// return response; -// } -// -// static Future sendNotificationToken({ -// required Map data, -// }) async { -// final response = await HTTPService().postRequest( -// path: APIConstants.notificationToken, -// body: data, -// showServerMessage: false, -// expectedResponseModel: (json) { -// bool checked = false; -// if (json != null) { -// if (json['success']) { -// checked = true; -// } -// } -// return checked; -// }); -// return response; -// } -// -// static Future logout() async { -// final response = await HTTPService().postRequest( -// path: APIConstants.logout, -// expectedResponseModel: (json) { -// bool checked = false; -// // print(json); -// if (json != null) { -// if (json['success']) { -// checked = true; -// } -// } -// return checked; -// }); -// return response; -// } -// -// static Future deleteAccount() async { -// final response = await HTTPService().postRequest( -// path: APIConstants.deleteAccount, -// expectedResponseModel: (json) { -// bool checked = false; -// if (json != null) { -// if (json['success']) { -// checked = true; -// } -// } -// return checked; -// }); -// return response; -// } - -// static Future refreshToken(Map data) async { -// final response = await HTTPService().postRequest( -// path: APIConstants.refreshToken, -// showServerMessage: false, -// body: data, -// expectedResponseModel: (json) { -// Token token = Token.fromJson(json); -// return token; -// }); -// return response; -// } } diff --git a/lib/services/api/http_service.dart b/lib/services/api/http_service.dart index ffbf72f..e3490f0 100644 --- a/lib/services/api/http_service.dart +++ b/lib/services/api/http_service.dart @@ -2,23 +2,24 @@ import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:syncrow_app/services/api/api_links_endpoints.dart'; import 'package:syncrow_app/services/api/http_interceptor.dart'; +import 'package:syncrow_app/services/api/network_exception.dart'; import 'package:syncrow_app/services/locator.dart'; class HTTPService { - late Dio client = serviceLocator.get(); + final Dio client = serviceLocator.get(); // final navigatorKey = GlobalKey(); String certificateString = ""; - Dio setupDioClient() { - client = Dio( + static Dio setupDioClient() { + Dio client = Dio( BaseOptions( baseUrl: ApiEndpoints.baseUrl, receiveDataWhenStatusError: true, followRedirects: false, - connectTimeout: const Duration(minutes: 1), - receiveTimeout: const Duration(minutes: 1), + connectTimeout: const Duration(seconds: 60), + receiveTimeout: const Duration(seconds: 60), ), ); @@ -53,16 +54,17 @@ class HTTPService { bool showServerMessage = true, required T Function(dynamic) expectedResponseModel}) async { try { - final response = await client.post(path, - data: body, queryParameters: queryParameters, options: options); - print("post response is $response"); + final response = await client.post( + path, + data: body, + queryParameters: queryParameters, + options: options, + ); debugPrint("status code is ${response.statusCode}"); debugPrint("response data is ${response.data}"); return expectedResponseModel(response.data); - } catch (error) { - debugPrint("******* Error ********"); - debugPrint(error.toString()); - rethrow; + } on DioException catch (error) { + throw ServerFailure.fromDioError(error); } } diff --git a/lib/services/api/network_exception.dart b/lib/services/api/network_exception.dart index 7eaee63..d117750 100644 --- a/lib/services/api/network_exception.dart +++ b/lib/services/api/network_exception.dart @@ -17,12 +17,12 @@ class ServerFailure extends Failure { factory ServerFailure.fromDioError(DioException dioError) { switch (dioError.type) { case DioExceptionType.connectionTimeout: - return ServerFailure("Connection timeout with ApiServer."); + return ServerFailure("Connection timeout with the Server."); case DioExceptionType.sendTimeout: - return ServerFailure("Send timeout with ApiServer."); + return ServerFailure("Send timeout with the Server."); case DioExceptionType.receiveTimeout: - return ServerFailure("Receive timeout with ApiServer."); + return ServerFailure("Receive timeout with the Server."); case DioExceptionType.badCertificate: return ServerFailure("Bad certificate!"); diff --git a/lib/services/locator.dart b/lib/services/locator.dart index 56b4cf0..da6399a 100644 --- a/lib/services/locator.dart +++ b/lib/services/locator.dart @@ -9,5 +9,5 @@ initialSetup() { serviceLocator.registerSingleton(HTTPInterceptor()); //Base classes - serviceLocator.registerSingleton(HTTPService().setupDioClient()); + serviceLocator.registerSingleton(HTTPService.setupDioClient()); } diff --git a/lib/utils/helpers/parser_helper.dart b/lib/utils/helpers/parser_helper.dart index 0ae3a5e..11d7d3b 100644 --- a/lib/utils/helpers/parser_helper.dart +++ b/lib/utils/helpers/parser_helper.dart @@ -36,7 +36,7 @@ class ParserHelper { } static String roundArea(dynamic value) { - if (value.isNotEmpty && value.contains(' ')) { + if (value.accessTokenIsNotEmpty && value.contains(' ')) { List split = value!.split(' '); String formattedArea = ParserHelper.roundNumber(split[0]);