feat(apps): add router auth protection

This commit is contained in:
qzl
2026-02-25 15:25:31 +08:00
parent 8c1dfa9987
commit d3bdb3ab4f
4 changed files with 180 additions and 87 deletions
+114 -76
View File
@@ -1,5 +1,7 @@
import 'package:go_router/go_router.dart';
import '../../features/auth/presentation/bloc/auth_bloc.dart';
import '../../features/auth/presentation/bloc/auth_state.dart';
import 'go_router_refresh_stream.dart';
import '../../features/auth/ui/screens/login_email_screen.dart';
import '../../features/auth/ui/screens/login_password_screen.dart';
import '../../features/auth/ui/screens/login_code_screen.dart';
@@ -20,78 +22,114 @@ import '../../features/settings/ui/screens/features_screen.dart';
import '../../features/settings/ui/screens/memory_screen.dart';
import '../../features/settings/ui/screens/account_screen.dart';
final appRouter = GoRouter(
initialLocation: '/',
routes: [
GoRoute(path: '/', builder: (context, state) => const LoginEmailScreen()),
GoRoute(
path: '/login/password',
builder: (context, state) => const LoginPasswordScreen(),
),
GoRoute(
path: '/login/code',
builder: (context, state) => const LoginCodeScreen(),
),
GoRoute(
path: '/register',
builder: (context, state) => const RegisterScreen(),
),
GoRoute(
path: '/register/step2',
builder: (context, state) => const RegisterStep2Screen(),
),
GoRoute(path: '/home', builder: (context, state) => const HomeScreen()),
GoRoute(
path: '/messages/invites',
builder: (context, state) => const MessageInviteListScreen(),
),
GoRoute(
path: '/messages/invites/:id',
builder: (context, state) => const MessageInviteDetailScreen(),
),
GoRoute(
path: '/contacts',
builder: (context, state) => const ContactsScreen(),
),
GoRoute(
path: '/contacts/add',
builder: (context, state) => const AddContactScreen(),
),
GoRoute(
path: '/calendar/dayweek',
builder: (context, state) => const CalendarDayWeekScreen(),
),
GoRoute(
path: '/calendar/month',
builder: (context, state) => const CalendarMonthScreen(),
),
GoRoute(
path: '/calendar/events/:id',
builder: (context, state) => const CalendarEventDetailScreen(),
),
GoRoute(
path: '/todo',
builder: (context, state) => const TodoQuadrantsScreen(),
),
GoRoute(
path: '/todo/:id',
builder: (context, state) => const TodoDetailScreen(),
),
GoRoute(
path: '/settings',
builder: (context, state) => const SettingsScreen(),
),
GoRoute(
path: '/settings/features',
builder: (context, state) => const FeaturesScreen(),
),
GoRoute(
path: '/settings/memory',
builder: (context, state) => const MemoryScreen(),
),
GoRoute(
path: '/settings/account',
builder: (context, state) => const AccountScreen(),
),
],
);
final _protectedRoutes = [
'/home',
'/contacts',
'/contacts/add',
'/calendar/dayweek',
'/calendar/month',
'/calendar/events',
'/todo',
'/settings',
'/settings/features',
'/settings/memory',
'/settings/account',
'/messages/invites',
];
GoRouter createAppRouter(AuthBloc authBloc) {
return GoRouter(
initialLocation: '/',
refreshListenable: GoRouterRefreshStream(authBloc.stream),
redirect: (context, state) {
final authState = authBloc.state;
final isAuthenticated = authState is AuthAuthenticated;
final isAuthRoute =
state.matchedLocation.startsWith('/login') ||
state.matchedLocation.startsWith('/register');
final isProtected = _protectedRoutes.any(
(route) => state.matchedLocation.startsWith(route),
);
if (!isAuthenticated && isProtected) {
return '/';
}
if (isAuthenticated && isAuthRoute) {
return '/home';
}
return null;
},
routes: [
GoRoute(path: '/', builder: (context, state) => const LoginEmailScreen()),
GoRoute(
path: '/login/password',
builder: (context, state) => const LoginPasswordScreen(),
),
GoRoute(
path: '/login/code',
builder: (context, state) => const LoginCodeScreen(),
),
GoRoute(
path: '/register',
builder: (context, state) => const RegisterScreen(),
),
GoRoute(
path: '/register/step2',
builder: (context, state) => const RegisterStep2Screen(),
),
GoRoute(path: '/home', builder: (context, state) => const HomeScreen()),
GoRoute(
path: '/messages/invites',
builder: (context, state) => const MessageInviteListScreen(),
),
GoRoute(
path: '/messages/invites/:id',
builder: (context, state) => const MessageInviteDetailScreen(),
),
GoRoute(
path: '/contacts',
builder: (context, state) => const ContactsScreen(),
),
GoRoute(
path: '/contacts/add',
builder: (context, state) => const AddContactScreen(),
),
GoRoute(
path: '/calendar/dayweek',
builder: (context, state) => const CalendarDayWeekScreen(),
),
GoRoute(
path: '/calendar/month',
builder: (context, state) => const CalendarMonthScreen(),
),
GoRoute(
path: '/calendar/events/:id',
builder: (context, state) => const CalendarEventDetailScreen(),
),
GoRoute(
path: '/todo',
builder: (context, state) => const TodoQuadrantsScreen(),
),
GoRoute(
path: '/todo/:id',
builder: (context, state) => const TodoDetailScreen(),
),
GoRoute(
path: '/settings',
builder: (context, state) => const SettingsScreen(),
),
GoRoute(
path: '/settings/features',
builder: (context, state) => const FeaturesScreen(),
),
GoRoute(
path: '/settings/memory',
builder: (context, state) => const MemoryScreen(),
),
GoRoute(
path: '/settings/account',
builder: (context, state) => const AccountScreen(),
),
],
);
}
@@ -0,0 +1,17 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
class GoRouterRefreshStream extends ChangeNotifier {
GoRouterRefreshStream(Stream<dynamic> stream) {
notifyListeners();
_subscription = stream.listen((_) => notifyListeners());
}
late final StreamSubscription<dynamic> _subscription;
@override
void dispose() {
_subscription.cancel();
super.dispose();
}
}