Interface de connexion avec connexion réussie au serveur

This commit is contained in:
DahoudG
2024-08-27 16:52:39 +00:00
parent 59331ddd7d
commit e233f9f392
9 changed files with 216 additions and 165 deletions

View File

@@ -10,6 +10,30 @@ class UserRemoteDataSource {
UserRemoteDataSource(this.client);
Future<UserModel> authenticateUser(String email, String password) async {
if (email.isEmpty || password.isEmpty) {
throw Exception('Email ou mot de passe vide');
}
final response = await client.post(
Uri.parse('${Urls.baseUrl}/users/authenticate'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'email': email,
'motDePasse': password,
}),
);
if (response.statusCode == 200) {
final jsonResponse = json.decode(response.body);
return UserModel.fromJson(jsonResponse);
} else if (response.statusCode == 401) {
throw AuthenticationException('Email ou mot de passe incorrect');
} else {
throw ServerException();
}
}
Future<UserModel> getUser(String id) async {
final response = await client.get(Uri.parse('${Urls.baseUrl}/user/$id'));

View File

@@ -1,6 +1,7 @@
import 'package:afterwork/domain/entities/user.dart';
import 'package:afterwork/domain/repositories/user_repository.dart';
import 'package:afterwork/data/datasources/user_remote_data_source.dart';
import 'package:afterwork/data/models/user_model.dart';
class UserRepositoryImpl implements UserRepository {
final UserRemoteDataSource remoteDataSource;
@@ -9,6 +10,12 @@ class UserRepositoryImpl implements UserRepository {
@override
Future<User> getUser(String id) async {
return await remoteDataSource.getUser(id);
UserModel userModel = await remoteDataSource.getUser(id);
return userModel; // Retourne un UserModel qui est un sous-type de User
}
Future<User> authenticateUser(String email, String password) async {
UserModel userModel = await remoteDataSource.authenticateUser(email, password);
return userModel; // Retourne un UserModel qui est un sous-type de User
}
}

View File

@@ -1,26 +1,37 @@
import '../../domain/entities/user.dart';
import 'package:afterwork/domain/entities/user.dart';
class UserModel extends User {
const UserModel({
required super.id,
required super.name,
required super.email,
});
required String id,
required String nom,
required String prenoms,
required String email,
required String motDePasse,
}) : super(
id: id,
nom: nom,
prenoms: prenoms,
email: email,
motDePasse: motDePasse,
);
factory UserModel.fromJson(Map<String, dynamic> json) {
return UserModel(
id: json['id'],
name: json['name'],
email: json['email'],
id: json['id'] ?? '',
nom: json['nom'] ?? '',
prenoms: json['prenoms'] ?? '',
email: json['email'] ?? '',
motDePasse: json['motDePasse'] ?? '',
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name,
'nom': nom,
'prenoms': prenoms,
'email': email,
'motDePasse': motDePasse,
};
}
}