feat(auth): restore authenticated user profile
This commit is contained in:
@@ -21,6 +21,14 @@ class LoginScreen extends StatefulWidget {
|
||||
class _LoginScreenState extends State<LoginScreen> {
|
||||
bool _connexionEnCours = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (AuthService.mode == AuthenticationMode.azure) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _restaurerSession());
|
||||
}
|
||||
}
|
||||
|
||||
void _ouvrirEtudiant() {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
@@ -39,7 +47,44 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
void _identifierParQr() {
|
||||
void _ouvrirPourRole(String roleCode) {
|
||||
switch (roleCode) {
|
||||
case "ETUDIANT":
|
||||
_ouvrirEtudiant();
|
||||
case "RESPONSABLE":
|
||||
_ouvrirResponsable();
|
||||
default:
|
||||
throw StateError("Role utilisateur non autorise.");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _restaurerSession() async {
|
||||
setState(() => _connexionEnCours = true);
|
||||
try {
|
||||
if (!await AuthService.hasMicrosoftAccount()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final reponse = await ApiClient.get("/auth/me");
|
||||
final profile = AuthService.setProfileFromApi(reponse);
|
||||
if (mounted) {
|
||||
_ouvrirPourRole(profile.roleCode);
|
||||
}
|
||||
} catch (_) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text("La session Microsoft doit etre renouvelee."),
|
||||
),
|
||||
);
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => _connexionEnCours = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _identifierParQr() async {
|
||||
if (_connexionEnCours) return;
|
||||
if (AuthService.mode == AuthenticationMode.azure) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -52,8 +97,7 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
return;
|
||||
}
|
||||
|
||||
AuthService.selectDemoStudent();
|
||||
_ouvrirEtudiant();
|
||||
await _connecterMicrosoft(responsableDemo: false);
|
||||
}
|
||||
|
||||
Future<void> _connecterMicrosoft({required bool responsableDemo}) async {
|
||||
@@ -64,27 +108,17 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
if (AuthService.mode == AuthenticationMode.demo) {
|
||||
if (responsableDemo) {
|
||||
AuthService.selectDemoResponsable();
|
||||
_ouvrirResponsable();
|
||||
} else {
|
||||
AuthService.selectDemoStudent();
|
||||
_ouvrirEtudiant();
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
await AuthService.signInWithMicrosoft();
|
||||
}
|
||||
|
||||
await AuthService.signInWithMicrosoft();
|
||||
final reponse = await ApiClient.get("/auth/me");
|
||||
final roleCode = _roleCode(reponse);
|
||||
final profile = AuthService.setProfileFromApi(reponse);
|
||||
if (!mounted) return;
|
||||
|
||||
switch (roleCode) {
|
||||
case "ETUDIANT":
|
||||
_ouvrirEtudiant();
|
||||
case "RESPONSABLE":
|
||||
_ouvrirResponsable();
|
||||
default:
|
||||
throw StateError("Role utilisateur non autorise.");
|
||||
}
|
||||
_ouvrirPourRole(profile.roleCode);
|
||||
} catch (error) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(
|
||||
@@ -97,25 +131,6 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
String _roleCode(dynamic reponse) {
|
||||
if (reponse is! Map || reponse["data"] is! Map) {
|
||||
throw StateError("Profil utilisateur invalide.");
|
||||
}
|
||||
|
||||
final data = reponse["data"] as Map;
|
||||
if (data["role"] is! Map) {
|
||||
throw StateError("Role utilisateur absent.");
|
||||
}
|
||||
|
||||
final role = data["role"] as Map;
|
||||
final code = role["code"];
|
||||
if (code is! String) {
|
||||
throw StateError("Role utilisateur invalide.");
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
|
||||
Reference in New Issue
Block a user