mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-16 01:56:19 +00:00
Added Login with email model instead of json encode
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
23
lib/features/auth/model/login_with_email_model.dart
Normal file
23
lib/features/auth/model/login_with_email_model.dart
Normal file
@ -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,
|
||||
};
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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<Token> 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']);
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user