111 lines
3.3 KiB
Dart
111 lines
3.3 KiB
Dart
import 'dart:convert';
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
import 'package:p_tits_pas/services/api/api_config.dart';
|
|
import '../models/user.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
|
class AuthResponse {
|
|
final String acessToken;
|
|
final String role;
|
|
|
|
AuthResponse({required this.acessToken, required this.role});
|
|
|
|
factory AuthResponse.fromJson(Map<String, dynamic> json) {
|
|
return AuthResponse(
|
|
acessToken: json['acessToken'],
|
|
role: json['role'],
|
|
);
|
|
}
|
|
}
|
|
|
|
class AuthService {
|
|
ApiConfig apiConfig = ApiConfig();
|
|
String baseUrl = ApiConfig.baseUrl;
|
|
final storage = const FlutterSecureStorage();
|
|
|
|
//login
|
|
Future<AuthResponse> login(String email, String password) async {
|
|
final response = await http.post(
|
|
Uri.parse('$baseUrl${ApiConfig.login}'),
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: jsonEncode({'email': email, 'password': password}),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
final authResponse = AuthResponse.fromJson(data);
|
|
|
|
await storage.write(key: 'access_token', value: authResponse.acessToken);
|
|
await storage.write(key: 'role', value: authResponse.role);
|
|
return authResponse;
|
|
} else {
|
|
throw Exception('Failed to login');
|
|
}
|
|
}
|
|
|
|
//register
|
|
Future<AppUser> register({
|
|
required String email,
|
|
required String password,
|
|
required String firstName,
|
|
required String lastName,
|
|
required String role,
|
|
}) async {
|
|
final response = await http.post(
|
|
Uri.parse('$baseUrl${ApiConfig.register}'),
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: jsonEncode({
|
|
'email': email,
|
|
'password': password,
|
|
'firstName': firstName,
|
|
'lastName': lastName,
|
|
'role': role,
|
|
}),
|
|
);
|
|
|
|
if (response.statusCode == 201) {
|
|
final data = jsonDecode(response.body);
|
|
return AppUser.fromJson(data['user']);
|
|
} else {
|
|
throw Exception('Failed to register');
|
|
}
|
|
}
|
|
|
|
/*static const String _usersKey = 'users';
|
|
static const String _parentsKey = 'parents';
|
|
static const String _childrenKey = 'children';
|
|
|
|
// Méthode pour se connecter (mode démonstration)
|
|
static Future<AppUser> login(String email, String password) async {
|
|
await Future.delayed(const Duration(seconds: 1)); // Simule un délai de traitement
|
|
throw Exception('Mode démonstration - Connexion désactivée');
|
|
}
|
|
|
|
// Méthode pour s'inscrire (mode démonstration)
|
|
static Future<AppUser> register({
|
|
required String email,
|
|
required String password,
|
|
required String firstName,
|
|
required String lastName,
|
|
required String role,
|
|
}) async {
|
|
await Future.delayed(const Duration(seconds: 1)); // Simule un délai de traitement
|
|
throw Exception('Mode démonstration - Inscription désactivée');
|
|
}
|
|
|
|
// Méthode pour se déconnecter (mode démonstration)
|
|
static Future<void> logout() async {
|
|
// Ne fait rien en mode démonstration
|
|
}
|
|
|
|
// Méthode pour vérifier si l'utilisateur est connecté (mode démonstration)
|
|
static Future<bool> isLoggedIn() async {
|
|
return false; // Toujours non connecté en mode démonstration
|
|
}
|
|
|
|
// Méthode pour récupérer l'utilisateur connecté (mode démonstration)
|
|
static Future<AppUser?> getCurrentUser() async {
|
|
return null; // Aucun utilisateur en mode démonstration
|
|
}*/
|
|
} |