Added Login with email model instead of json encode

This commit is contained in:
Mohammad Salameh
2024-03-10 10:20:06 +03:00
parent 5a7787b42d
commit 2c4543e83f
6 changed files with 57 additions and 30 deletions
+10 -17
View File
@@ -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<AuthState> {
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);
}
}
}
@@ -0,0 +1,23 @@
class LoginWithEmailModel {
final String email;
final String password;
LoginWithEmailModel({
required this.email,
required this.password,
});
factory LoginWithEmailModel.fromJson(Map<String, dynamic> json) {
return LoginWithEmailModel(
email: json['email'],
password: json['password'],
);
}
Map<String, dynamic> toJson() {
return {
'email': email,
'password': password,
};
}
}
+12
View File
@@ -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<String, String> toJson() => {loginRefreshToken: refreshToken};
Map<String, dynamic> decodeToken() {
final parts = accessToken.split('.');
if (parts.length != 3) {
throw Exception('invalid access token');
}
final payload = decodeBase64(parts[1]);
return json.decode(payload);
}
}
+7 -3
View File
@@ -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<String, dynamic> json) {
factory UserModel.fromToken(Token token) {
Map<String, dynamic> 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,