mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-16 18:16:21 +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/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.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/token.dart';
|
||||||
import 'package:syncrow_app/features/auth/model/user_model.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/authentication_api.dart';
|
||||||
import 'package:syncrow_app/services/api/network_exception.dart';
|
import 'package:syncrow_app/services/api/network_exception.dart';
|
||||||
import 'package:syncrow_app/utils/helpers/decode_base64.dart';
|
|
||||||
|
|
||||||
part 'auth_state.dart';
|
part 'auth_state.dart';
|
||||||
|
|
||||||
@ -44,27 +43,21 @@ class AuthCubit extends Cubit<AuthState> {
|
|||||||
emit(AuthLoading());
|
emit(AuthLoading());
|
||||||
try {
|
try {
|
||||||
token = await AuthenticationAPI.loginWithEmail(
|
token = await AuthenticationAPI.loginWithEmail(
|
||||||
|
model: LoginWithEmailModel(
|
||||||
email: emailController.text.toLowerCase(),
|
email: emailController.text.toLowerCase(),
|
||||||
password: passwordController.text,
|
password: passwordController.text,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (token.accessTokenIsNotEmpty) {
|
if (token.accessTokenIsNotEmpty) {
|
||||||
final parts = token.accessToken.split('.');
|
//Map dictionary
|
||||||
if (parts.length != 3) {
|
user = UserModel.fromToken(token);
|
||||||
throw Exception('invalid access token');
|
|
||||||
}
|
|
||||||
final payload = decodeBase64(parts[1]);
|
|
||||||
final payloadMap = json.decode(payload); //Map dictionary
|
|
||||||
user = UserModel.fromToken(payloadMap);
|
|
||||||
emit(AuthSuccess());
|
emit(AuthSuccess());
|
||||||
} else {
|
} else {
|
||||||
emit(AuthError('Something went wrong'));
|
emit(AuthError('Something went wrong'));
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} on DioException catch (e) {
|
||||||
if (e is ServerFailure) {
|
throw ServerFailure.fromDioError(e);
|
||||||
emit(AuthError(e.errMessage));
|
|
||||||
}
|
|
||||||
emit(AuthError(e.toString()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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:flutter_secure_storage/flutter_secure_storage.dart';
|
||||||
|
import 'package:syncrow_app/utils/helpers/decode_base64.dart';
|
||||||
|
|
||||||
class Token {
|
class Token {
|
||||||
static const String loginAccessToken = 'access_token';
|
static const String loginAccessToken = 'access_token';
|
||||||
@ -30,4 +33,13 @@ class Token {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Map<String, String> toJson() => {loginRefreshToken: refreshToken};
|
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 {
|
class UserModel {
|
||||||
final String? id;
|
final String? id;
|
||||||
final String? email;
|
final String? email;
|
||||||
@ -43,10 +45,12 @@ class UserModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//from token
|
//from token
|
||||||
factory UserModel.fromToken(Map<String, dynamic> json) {
|
factory UserModel.fromToken(Token token) {
|
||||||
|
Map<String, dynamic> tempJson = token.decodeToken();
|
||||||
|
|
||||||
return UserModel(
|
return UserModel(
|
||||||
id: json['userId'].toString(),
|
id: tempJson['userId'].toString(),
|
||||||
email: json['email'],
|
email: tempJson['email'],
|
||||||
name: null,
|
name: null,
|
||||||
photoUrl: null,
|
photoUrl: null,
|
||||||
phoneNumber: null,
|
phoneNumber: null,
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import 'dart:convert';
|
|
||||||
|
|
||||||
import 'package:flutter/cupertino.dart';
|
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/token.dart';
|
||||||
import 'package:syncrow_app/features/auth/model/verify_code.dart';
|
import 'package:syncrow_app/features/auth/model/verify_code.dart';
|
||||||
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
|
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
|
||||||
@ -20,13 +19,10 @@ class AuthenticationAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Future<Token> loginWithEmail(
|
static Future<Token> loginWithEmail(
|
||||||
{required String email, required String password}) async {
|
{required LoginWithEmailModel model}) async {
|
||||||
final response = await HTTPService().post(
|
final response = await HTTPService().post(
|
||||||
path: ApiEndpoints.login,
|
path: ApiEndpoints.login,
|
||||||
body: jsonEncode({
|
body: model.toJson(),
|
||||||
"email": email,
|
|
||||||
"password": password,
|
|
||||||
}),
|
|
||||||
showServerMessage: false,
|
showServerMessage: false,
|
||||||
expectedResponseModel: (json) {
|
expectedResponseModel: (json) {
|
||||||
Token token = Token.fromJson(json['data']);
|
Token token = Token.fromJson(json['data']);
|
||||||
|
@ -2,7 +2,6 @@ import 'package:dio/dio.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:syncrow_app/services/api/api_links_endpoints.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/http_interceptor.dart';
|
||||||
import 'package:syncrow_app/services/api/network_exception.dart';
|
|
||||||
import 'package:syncrow_app/services/locator.dart';
|
import 'package:syncrow_app/services/locator.dart';
|
||||||
|
|
||||||
class HTTPService {
|
class HTTPService {
|
||||||
@ -63,8 +62,8 @@ class HTTPService {
|
|||||||
debugPrint("status code is ${response.statusCode}");
|
debugPrint("status code is ${response.statusCode}");
|
||||||
debugPrint("response data is ${response.data}");
|
debugPrint("response data is ${response.data}");
|
||||||
return expectedResponseModel(response.data);
|
return expectedResponseModel(response.data);
|
||||||
} on DioException catch (error) {
|
} catch (error) {
|
||||||
throw ServerFailure.fromDioError(error);
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user