feat: Implement authentication service and login handling with role-based navigation

This commit is contained in:
Hanim
2025-09-03 11:03:53 +02:00
parent 864d72eb40
commit 9fd6cb7b76
10 changed files with 242 additions and 11 deletions
+21
View File
@@ -0,0 +1,21 @@
class ApiConfig {
// static const String baseUrl = 'https://ynov.ptits-pas.fr/api/v1';
static const String baseUrl = 'http://localhost:3000/api/v1';
// Auth endpoints
static const String login = '/auth/login';
static const String register = '/auth/register';
static const String refreshToken = '/auth/refresh';
// Users endpoints
static const String users = '/users';
static const String userProfile = '/users/profile';
static const String userChildren = '/users/children';
// Dashboard endpoints
static const String dashboard = '/dashboard';
static const String events = '/events';
static const String contracts = '/contracts';
static const String conversations = '/conversations';
static const String notifications = '/notifications';
}
+72 -3
View File
@@ -1,9 +1,78 @@
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
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 {
static const String _usersKey = 'users';
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';
@@ -38,5 +107,5 @@ class AuthService {
// 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
}
}*/
}
@@ -0,0 +1,20 @@
import 'package:flutter/cupertino.dart';
class NavigationService {
static void handleLoginSuccess(BuildContext context, String role) {
switch (role) {
case 'admin':
Navigator.pushReplacementNamed(context, '/admin_dashboard');
break;
case 'gestionnaire':
Navigator.pushReplacementNamed(context, '/gestionnaire_dashboard');
break;
case 'parent':
Navigator.pushReplacementNamed(context, '/parent-dashboard');
break;
case 'assistante_maternelle':
Navigator.pushReplacementNamed(context, '/assistante_maternelle_dashboard');
break;
}
}
}