From 2c4543e83ffd9257ee859f64a27ecaf4719aa7dc Mon Sep 17 00:00:00 2001 From: Mohammad Salameh Date: Sun, 10 Mar 2024 10:20:06 +0300 Subject: [PATCH] Added Login with email model instead of json encode --- lib/features/auth/bloc/auth_cubit.dart | 27 +++++++------------ .../auth/model/login_with_email_model.dart | 23 ++++++++++++++++ lib/features/auth/model/token.dart | 12 +++++++++ lib/features/auth/model/user_model.dart | 10 ++++--- lib/services/api/authentication_api.dart | 10 +++---- lib/services/api/http_service.dart | 5 ++-- 6 files changed, 57 insertions(+), 30 deletions(-) create mode 100644 lib/features/auth/model/login_with_email_model.dart diff --git a/lib/features/auth/bloc/auth_cubit.dart b/lib/features/auth/bloc/auth_cubit.dart index 7e4709b..e82e3d3 100644 --- a/lib/features/auth/bloc/auth_cubit.dart +++ b/lib/features/auth/bloc/auth_cubit.dart @@ -1,13 +1,12 @@ -import 'dart:convert'; - +import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.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/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'; @@ -44,27 +43,21 @@ class AuthCubit extends Cubit { emit(AuthLoading()); try { token = await AuthenticationAPI.loginWithEmail( - email: emailController.text.toLowerCase(), - password: passwordController.text, + model: LoginWithEmailModel( + email: emailController.text.toLowerCase(), + password: passwordController.text, + ), ); 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); + //Map dictionary + user = UserModel.fromToken(token); emit(AuthSuccess()); } else { emit(AuthError('Something went wrong')); } - } catch (e) { - if (e is ServerFailure) { - emit(AuthError(e.errMessage)); - } - emit(AuthError(e.toString())); + } on DioException catch (e) { + throw ServerFailure.fromDioError(e); } } } diff --git a/lib/features/auth/model/login_with_email_model.dart b/lib/features/auth/model/login_with_email_model.dart new file mode 100644 index 0000000..c387b0d --- /dev/null +++ b/lib/features/auth/model/login_with_email_model.dart @@ -0,0 +1,23 @@ +class LoginWithEmailModel { + final String email; + final String password; + + LoginWithEmailModel({ + required this.email, + required this.password, + }); + + factory LoginWithEmailModel.fromJson(Map json) { + return LoginWithEmailModel( + email: json['email'], + password: json['password'], + ); + } + + Map toJson() { + return { + 'email': email, + 'password': password, + }; + } +} diff --git a/lib/features/auth/model/token.dart b/lib/features/auth/model/token.dart index d63fdcd..c87bf82 100644 --- a/lib/features/auth/model/token.dart +++ b/lib/features/auth/model/token.dart @@ -1,4 +1,7 @@ +import 'dart:convert'; + import 'package:flutter_secure_storage/flutter_secure_storage.dart'; +import 'package:syncrow_app/utils/helpers/decode_base64.dart'; class Token { static const String loginAccessToken = 'access_token'; @@ -30,4 +33,13 @@ class Token { } Map toJson() => {loginRefreshToken: refreshToken}; + + Map decodeToken() { + final parts = accessToken.split('.'); + if (parts.length != 3) { + throw Exception('invalid access token'); + } + final payload = decodeBase64(parts[1]); + return json.decode(payload); + } } diff --git a/lib/features/auth/model/user_model.dart b/lib/features/auth/model/user_model.dart index bb052da..baf44af 100644 --- a/lib/features/auth/model/user_model.dart +++ b/lib/features/auth/model/user_model.dart @@ -1,3 +1,5 @@ +import 'package:syncrow_app/features/auth/model/token.dart'; + class UserModel { final String? id; final String? email; @@ -43,10 +45,12 @@ class UserModel { } //from token - factory UserModel.fromToken(Map json) { + factory UserModel.fromToken(Token token) { + Map tempJson = token.decodeToken(); + return UserModel( - id: json['userId'].toString(), - email: json['email'], + id: tempJson['userId'].toString(), + email: tempJson['email'], name: null, photoUrl: null, phoneNumber: null, diff --git a/lib/services/api/authentication_api.dart b/lib/services/api/authentication_api.dart index 5b8b661..62d406c 100644 --- a/lib/services/api/authentication_api.dart +++ b/lib/services/api/authentication_api.dart @@ -1,6 +1,5 @@ -import 'dart:convert'; - import 'package:flutter/cupertino.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/verify_code.dart'; import 'package:syncrow_app/services/api/api_links_endpoints.dart'; @@ -20,13 +19,10 @@ class AuthenticationAPI { } static Future loginWithEmail( - {required String email, required String password}) async { + {required LoginWithEmailModel model}) async { final response = await HTTPService().post( path: ApiEndpoints.login, - body: jsonEncode({ - "email": email, - "password": password, - }), + body: model.toJson(), showServerMessage: false, expectedResponseModel: (json) { Token token = Token.fromJson(json['data']); diff --git a/lib/services/api/http_service.dart b/lib/services/api/http_service.dart index e3490f0..ec76fa5 100644 --- a/lib/services/api/http_service.dart +++ b/lib/services/api/http_service.dart @@ -2,7 +2,6 @@ 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 { @@ -63,8 +62,8 @@ class HTTPService { debugPrint("status code is ${response.statusCode}"); debugPrint("response data is ${response.data}"); return expectedResponseModel(response.data); - } on DioException catch (error) { - throw ServerFailure.fromDioError(error); + } catch (error) { + rethrow; } }