fix(auth): connexion admin - token snake_case, routes GoRouter, profil (Closes #84)
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
480f4a9396
commit
18af5c9034
@ -19,6 +19,8 @@ import '../screens/auth/am_register_step2_screen.dart';
|
||||
import '../screens/auth/am_register_step3_screen.dart';
|
||||
import '../screens/auth/am_register_step4_screen.dart';
|
||||
import '../screens/home/home_screen.dart';
|
||||
import '../screens/administrateurs/admin_dashboardScreen.dart';
|
||||
import '../screens/home/parent_screen/ParentDashboardScreen.dart';
|
||||
import '../screens/unknown_screen.dart';
|
||||
|
||||
// --- Provider Instances ---
|
||||
@ -47,6 +49,18 @@ class AppRouter {
|
||||
path: '/home',
|
||||
builder: (BuildContext context, GoRouterState state) => const HomeScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/admin-dashboard',
|
||||
builder: (BuildContext context, GoRouterState state) => const AdminDashboardScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/parent-dashboard',
|
||||
builder: (BuildContext context, GoRouterState state) => const ParentDashboardScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/am-dashboard',
|
||||
builder: (BuildContext context, GoRouterState state) => const HomeScreen(),
|
||||
),
|
||||
|
||||
// --- Parent Registration Flow ---
|
||||
ShellRoute(
|
||||
|
||||
@ -20,8 +20,12 @@ class AppUser {
|
||||
id: json['id'] as String,
|
||||
email: json['email'] as String,
|
||||
role: json['role'] as String,
|
||||
createdAt: DateTime.parse(json['createdAt'] as String),
|
||||
updatedAt: DateTime.parse(json['updatedAt'] as String),
|
||||
createdAt: json['createdAt'] != null
|
||||
? DateTime.parse(json['createdAt'] as String)
|
||||
: DateTime.now(),
|
||||
updatedAt: json['updatedAt'] != null
|
||||
? DateTime.parse(json['updatedAt'] as String)
|
||||
: DateTime.now(),
|
||||
changementMdpObligatoire: json['changement_mdp_obligatoire'] as bool? ?? false,
|
||||
);
|
||||
}
|
||||
|
||||
@ -116,21 +116,23 @@ class _LoginPageState extends State<LoginScreen> with WidgetsBindingObserver {
|
||||
}
|
||||
}
|
||||
|
||||
/// Redirige l'utilisateur selon son rôle
|
||||
/// Redirige l'utilisateur selon son rôle (GoRouter : context.go).
|
||||
void _redirectUserByRole(String role) {
|
||||
setState(() => _isLoading = false);
|
||||
switch (role.toLowerCase()) {
|
||||
case 'super_admin':
|
||||
case 'administrateur':
|
||||
case 'gestionnaire':
|
||||
Navigator.pushReplacementNamed(context, '/admin-dashboard');
|
||||
context.go('/admin-dashboard');
|
||||
break;
|
||||
case 'parent':
|
||||
Navigator.pushReplacementNamed(context, '/parent-dashboard');
|
||||
context.go('/parent-dashboard');
|
||||
break;
|
||||
case 'assistante_maternelle':
|
||||
Navigator.pushReplacementNamed(context, '/am-dashboard');
|
||||
context.go('/am-dashboard');
|
||||
break;
|
||||
default:
|
||||
Navigator.pushReplacementNamed(context, '/home');
|
||||
context.go('/home');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -23,13 +23,15 @@ class AuthService {
|
||||
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
final data = jsonDecode(response.body);
|
||||
// API renvoie access_token / refresh_token (snake_case)
|
||||
final accessToken = data['access_token'] as String? ?? data['accessToken'] as String?;
|
||||
final refreshToken = data['refresh_token'] as String? ?? data['refreshToken'] as String?;
|
||||
if (accessToken == null) throw Exception('Token absent dans la réponse serveur');
|
||||
|
||||
// Stocker les tokens
|
||||
await TokenService.saveToken(data['accessToken']);
|
||||
await TokenService.saveRefreshToken(data['refreshToken']);
|
||||
await TokenService.saveToken(accessToken);
|
||||
await TokenService.saveRefreshToken(refreshToken ?? '');
|
||||
|
||||
// Récupérer le profil utilisateur pour avoir toutes les infos
|
||||
final user = await _fetchUserProfile(data['accessToken']);
|
||||
final user = await _fetchUserProfile(accessToken);
|
||||
|
||||
// Stocker l'utilisateur en cache
|
||||
await _saveCurrentUser(user);
|
||||
@ -80,8 +82,9 @@ class AuthService {
|
||||
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.changePasswordRequired}'),
|
||||
headers: ApiConfig.authHeaders(token),
|
||||
body: jsonEncode({
|
||||
'currentPassword': currentPassword,
|
||||
'newPassword': newPassword,
|
||||
'mot_de_passe_actuel': currentPassword,
|
||||
'nouveau_mot_de_passe': newPassword,
|
||||
'confirmation_mot_de_passe': newPassword,
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user