Phase B+1: Bootstrap-Splash + Logout im Drawer
- AuthBootstrapping als neuer Initial-State im AuthBloc. Beim Cold-Start bleibt die App im Splash, bis restoreSession entweder Authenticated oder Unauthenticated emittiert — kein sichtbarer LoginPage-Flash mehr für Nutzer mit gespeicherter Session. - LoginEnforcer rendert für AuthBootstrapping ein eigenes Splash-Widget mit Logo + Spinner, für Unauthenticated weiterhin die LoginPage. - AuthBloc._handleRestore emittiert Unauthenticated explizit, wenn restoreSession false liefert oder wirft — sonst bliebe der Bootstrap- State hängen. - HomeAppDrawer zeigt jetzt displayName + Personalnummer aus dem Authenticated-State im Header und bekommt einen Abmelden-Eintrag unten (rot, Confirm-Dialog), der LogoutRequested feuert. Der Provider löscht den Refresh-Token aus der Secure Storage und der LoginEnforcer routet automatisch zurück auf die LoginPage.
This commit is contained in:
@ -18,7 +18,7 @@ import 'package:hl_lieferservice/widget/operations/bloc/operation_event.dart';
|
|||||||
/// `ProviderSessionChanged` übersetzt und vom Bloc in Zustände überführt.
|
/// `ProviderSessionChanged` übersetzt und vom Bloc in Zustände überführt.
|
||||||
class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||||
AuthBloc({required this.tokenProvider, required this.operationBloc})
|
AuthBloc({required this.tokenProvider, required this.operationBloc})
|
||||||
: super(Unauthenticated()) {
|
: super(AuthBootstrapping()) {
|
||||||
on<LoginRequested>(_handleLogin);
|
on<LoginRequested>(_handleLogin);
|
||||||
on<LogoutRequested>(_handleLogout);
|
on<LogoutRequested>(_handleLogout);
|
||||||
on<RestoreSessionRequested>(_handleRestore);
|
on<RestoreSessionRequested>(_handleRestore);
|
||||||
@ -75,14 +75,18 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
|||||||
Emitter<AuthState> emit,
|
Emitter<AuthState> emit,
|
||||||
) async {
|
) async {
|
||||||
try {
|
try {
|
||||||
await tokenProvider.restoreSession();
|
final restored = await tokenProvider.restoreSession();
|
||||||
// Erfolg landet via Stream als ProviderSessionChanged.loggedIn.
|
if (!restored) {
|
||||||
// Misserfolg: bleibt bei Unauthenticated — kein Snackbar, das
|
// Kein gespeicherter Refresh-Token oder Refresh fehlgeschlagen:
|
||||||
// ist der normale Cold-Start-Pfad.
|
// Vom Splash zur LoginPage übergehen. Kein Snackbar — das ist
|
||||||
|
// der normale Cold-Start-Pfad.
|
||||||
|
emit(Unauthenticated());
|
||||||
|
}
|
||||||
|
// Erfolg landet via Stream als ProviderSessionChanged.loggedIn,
|
||||||
|
// der Handler emittiert Authenticated.
|
||||||
} catch (err, st) {
|
} catch (err, st) {
|
||||||
debugPrint('Restore-Session fehlgeschlagen: $err\n$st');
|
debugPrint('Restore-Session fehlgeschlagen: $err\n$st');
|
||||||
// State unverändert (Unauthenticated). Kein Snackbar — das
|
emit(Unauthenticated());
|
||||||
// wäre für den Cold-Start zu viel Lärm.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,12 @@ import 'package:hl_lieferservice/feature/authentication/model/user.dart';
|
|||||||
|
|
||||||
abstract class AuthState {}
|
abstract class AuthState {}
|
||||||
|
|
||||||
|
/// Initialer State beim App-Start: der Refresh-Token aus der Secure
|
||||||
|
/// Storage wird gerade gegen das Issuer-Endpoint geprüft. Die UI zeigt
|
||||||
|
/// in dieser Zeit einen Splash statt LoginPage, damit Nutzer mit
|
||||||
|
/// gespeicherter Session keinen sichtbaren Login-Flash sehen.
|
||||||
|
class AuthBootstrapping extends AuthState {}
|
||||||
|
|
||||||
class Unauthenticated extends AuthState {
|
class Unauthenticated extends AuthState {
|
||||||
final bool sessionExpired;
|
final bool sessionExpired;
|
||||||
Unauthenticated({this.sessionExpired = false});
|
Unauthenticated({this.sessionExpired = false});
|
||||||
|
|||||||
@ -1,10 +1,18 @@
|
|||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:hl_lieferservice/feature/authentication/presentation/login_page.dart';
|
import 'package:hl_lieferservice/feature/authentication/presentation/login_page.dart';
|
||||||
|
|
||||||
import '../bloc/auth_bloc.dart';
|
import '../bloc/auth_bloc.dart';
|
||||||
import '../bloc/auth_state.dart';
|
import '../bloc/auth_state.dart';
|
||||||
|
|
||||||
|
/// Routet die App zwischen Bootstrap-Splash, Login-Page und der
|
||||||
|
/// eigentlichen UI:
|
||||||
|
/// * `AuthBootstrapping` → Splash (verhindert Login-Page-Flash beim
|
||||||
|
/// Cold-Start, während der Refresh-Token gegen Keycloak gegengeprüft
|
||||||
|
/// wird).
|
||||||
|
/// * `Authenticated` → `child` (= reguläre App).
|
||||||
|
/// * sonst → LoginPage; `sessionExpired`-Banner wenn ein Refresh
|
||||||
|
/// serverseitig abgewiesen wurde.
|
||||||
class LoginEnforcer extends StatelessWidget {
|
class LoginEnforcer extends StatelessWidget {
|
||||||
final Widget child;
|
final Widget child;
|
||||||
|
|
||||||
@ -17,10 +25,37 @@ class LoginEnforcer extends StatelessWidget {
|
|||||||
if (state is Authenticated) {
|
if (state is Authenticated) {
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
|
if (state is AuthBootstrapping) {
|
||||||
|
return const _AuthBootstrapSplash();
|
||||||
|
}
|
||||||
final expired = state is Unauthenticated && state.sessionExpired;
|
final expired = state is Unauthenticated && state.sessionExpired;
|
||||||
return LoginPage(sessionExpired: expired);
|
return LoginPage(sessionExpired: expired);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class _AuthBootstrapSplash extends StatelessWidget {
|
||||||
|
const _AuthBootstrapSplash();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
body: Center(
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 60),
|
||||||
|
child: Image.asset(
|
||||||
|
'assets/holzleitner_Logo_2017_RZ_transparent.png',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 32),
|
||||||
|
const CircularProgressIndicator(),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:hl_lieferservice/feature/authentication/bloc/auth_bloc.dart';
|
||||||
|
import 'package:hl_lieferservice/feature/authentication/bloc/auth_event.dart';
|
||||||
|
import 'package:hl_lieferservice/feature/authentication/bloc/auth_state.dart';
|
||||||
import 'package:hl_lieferservice/feature/car_selection/bloc/bloc.dart';
|
import 'package:hl_lieferservice/feature/car_selection/bloc/bloc.dart';
|
||||||
import 'package:hl_lieferservice/feature/car_selection/bloc/events.dart';
|
import 'package:hl_lieferservice/feature/car_selection/bloc/events.dart';
|
||||||
import 'package:hl_lieferservice/feature/car_selection/bloc/state.dart';
|
import 'package:hl_lieferservice/feature/car_selection/bloc/state.dart';
|
||||||
@ -22,10 +25,46 @@ class HomeAppDrawer extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _confirmLogout(BuildContext context) async {
|
||||||
|
final authBloc = context.read<AuthBloc>();
|
||||||
|
final confirmed = await showDialog<bool>(
|
||||||
|
context: context,
|
||||||
|
builder: (dialogContext) => AlertDialog(
|
||||||
|
title: const Text("Abmelden"),
|
||||||
|
content: const Text(
|
||||||
|
"Möchten Sie sich wirklich abmelden? "
|
||||||
|
"Beim nächsten Start ist eine erneute Anmeldung erforderlich.",
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.of(dialogContext).pop(false),
|
||||||
|
child: const Text("Abbrechen"),
|
||||||
|
),
|
||||||
|
FilledButton(
|
||||||
|
style: FilledButton.styleFrom(backgroundColor: Colors.red),
|
||||||
|
onPressed: () => Navigator.of(dialogContext).pop(true),
|
||||||
|
child: const Text("Abmelden"),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (confirmed != true) return;
|
||||||
|
|
||||||
|
authBloc.add(const LogoutRequested());
|
||||||
|
|
||||||
|
// Drawer schließen, falls noch sichtbar — der State-Wechsel auf
|
||||||
|
// Unauthenticated leitet danach von selbst auf die LoginPage.
|
||||||
|
if (context.mounted && Scaffold.maybeOf(context)?.isDrawerOpen == true) {
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final theme = Theme.of(context);
|
final theme = Theme.of(context);
|
||||||
final carState = context.watch<CarSelectBloc>().state;
|
final carState = context.watch<CarSelectBloc>().state;
|
||||||
|
final authState = context.watch<AuthBloc>().state;
|
||||||
|
final authenticated = authState is Authenticated ? authState : null;
|
||||||
|
|
||||||
return Drawer(
|
return Drawer(
|
||||||
child: SafeArea(
|
child: SafeArea(
|
||||||
@ -40,14 +79,26 @@ class HomeAppDrawer extends StatelessWidget {
|
|||||||
mainAxisAlignment: MainAxisAlignment.end,
|
mainAxisAlignment: MainAxisAlignment.end,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
"Holzleitner",
|
authenticated?.displayName ?? "Holzleitner",
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
color: theme.colorScheme.onSecondary,
|
color: theme.colorScheme.onSecondary,
|
||||||
fontSize: 20,
|
fontSize: 20,
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 4),
|
if (authenticated != null) ...[
|
||||||
|
const SizedBox(height: 2),
|
||||||
|
Text(
|
||||||
|
"Personalnummer ${authenticated.personalnummer}",
|
||||||
|
style: TextStyle(
|
||||||
|
color: theme.colorScheme.onSecondary.withValues(
|
||||||
|
alpha: 0.85,
|
||||||
|
),
|
||||||
|
fontSize: 12,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
const SizedBox(height: 8),
|
||||||
if (carState is CarSelectComplete)
|
if (carState is CarSelectComplete)
|
||||||
Row(
|
Row(
|
||||||
children: [
|
children: [
|
||||||
@ -100,8 +151,16 @@ class HomeAppDrawer extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
const Spacer(),
|
const Spacer(),
|
||||||
const Divider(height: 1),
|
const Divider(height: 1),
|
||||||
|
ListTile(
|
||||||
|
leading: const Icon(Icons.logout, color: Colors.red),
|
||||||
|
title: const Text(
|
||||||
|
"Abmelden",
|
||||||
|
style: TextStyle(color: Colors.red),
|
||||||
|
),
|
||||||
|
onTap: () => _confirmLogout(context),
|
||||||
|
),
|
||||||
const Padding(
|
const Padding(
|
||||||
padding: EdgeInsets.all(12),
|
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||||
child: Text(
|
child: Text(
|
||||||
"Lieferservice-App",
|
"Lieferservice-App",
|
||||||
style: TextStyle(fontSize: 11, color: Colors.grey),
|
style: TextStyle(fontSize: 11, color: Colors.grey),
|
||||||
|
|||||||
Reference in New Issue
Block a user