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();
}
}
+24 -9
View File
@@ -1,21 +1,36 @@
import 'package:flutter/material.dart';
import 'core/theme/app_theme.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'core/di/injection.dart';
import 'core/router/app_router.dart';
import 'core/theme/app_theme.dart';
import 'features/auth/presentation/bloc/auth_bloc.dart';
import 'features/auth/presentation/bloc/auth_event.dart';
void main() {
runApp(const LinksyApp());
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await configureDependencies();
final authBloc = sl<AuthBloc>();
authBloc.add(AuthStarted());
runApp(LinksyApp(authBloc: authBloc));
}
class LinksyApp extends StatelessWidget {
const LinksyApp({super.key});
final AuthBloc authBloc;
const LinksyApp({super.key, required this.authBloc});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'Linksy',
debugShowCheckedModeBanner: false,
theme: AppTheme.light,
routerConfig: appRouter,
return BlocProvider<AuthBloc>.value(
value: authBloc,
child: MaterialApp.router(
title: 'Linksy',
debugShowCheckedModeBanner: false,
theme: AppTheme.light,
routerConfig: createAppRouter(authBloc),
),
);
}
}
+25 -2
View File
@@ -1,10 +1,27 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:social_app/main.dart';
import 'package:social_app/features/auth/presentation/bloc/auth_bloc.dart';
import 'package:social_app/features/auth/presentation/bloc/auth_state.dart';
class MockAuthBloc extends Mock implements AuthBloc {}
class FakeAuthState extends Fake implements AuthState {}
void main() {
setUpAll(() {
registerFallbackValue(FakeAuthState());
});
testWidgets('Login screen loads correctly', (WidgetTester tester) async {
await tester.pumpWidget(const LinksyApp());
final mockAuthBloc = MockAuthBloc();
when(() => mockAuthBloc.state).thenReturn(AuthInitial());
when(
() => mockAuthBloc.stream,
).thenAnswer((_) => Stream.value(AuthInitial()));
await tester.pumpWidget(LinksyApp(authBloc: mockAuthBloc));
expect(find.text('linksy'), findsOneWidget);
expect(find.text('继续'), findsOneWidget);
expect(find.text('还没有账号?去注册'), findsOneWidget);
@@ -13,7 +30,13 @@ void main() {
testWidgets('Main content is vertically centered above footer', (
WidgetTester tester,
) async {
await tester.pumpWidget(const LinksyApp());
final mockAuthBloc = MockAuthBloc();
when(() => mockAuthBloc.state).thenReturn(AuthInitial());
when(
() => mockAuthBloc.stream,
).thenAnswer((_) => Stream.value(AuthInitial()));
await tester.pumpWidget(LinksyApp(authBloc: mockAuthBloc));
final safeAreaRect = tester.getRect(find.byType(SafeArea));
final mainRect = tester.getRect(