feat(apps): add router auth protection
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import 'package:go_router/go_router.dart';
|
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_email_screen.dart';
|
||||||
import '../../features/auth/ui/screens/login_password_screen.dart';
|
import '../../features/auth/ui/screens/login_password_screen.dart';
|
||||||
import '../../features/auth/ui/screens/login_code_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/memory_screen.dart';
|
||||||
import '../../features/settings/ui/screens/account_screen.dart';
|
import '../../features/settings/ui/screens/account_screen.dart';
|
||||||
|
|
||||||
final appRouter = GoRouter(
|
final _protectedRoutes = [
|
||||||
initialLocation: '/',
|
'/home',
|
||||||
routes: [
|
'/contacts',
|
||||||
GoRoute(path: '/', builder: (context, state) => const LoginEmailScreen()),
|
'/contacts/add',
|
||||||
GoRoute(
|
'/calendar/dayweek',
|
||||||
path: '/login/password',
|
'/calendar/month',
|
||||||
builder: (context, state) => const LoginPasswordScreen(),
|
'/calendar/events',
|
||||||
),
|
'/todo',
|
||||||
GoRoute(
|
'/settings',
|
||||||
path: '/login/code',
|
'/settings/features',
|
||||||
builder: (context, state) => const LoginCodeScreen(),
|
'/settings/memory',
|
||||||
),
|
'/settings/account',
|
||||||
GoRoute(
|
'/messages/invites',
|
||||||
path: '/register',
|
];
|
||||||
builder: (context, state) => const RegisterScreen(),
|
|
||||||
),
|
GoRouter createAppRouter(AuthBloc authBloc) {
|
||||||
GoRoute(
|
return GoRouter(
|
||||||
path: '/register/step2',
|
initialLocation: '/',
|
||||||
builder: (context, state) => const RegisterStep2Screen(),
|
refreshListenable: GoRouterRefreshStream(authBloc.stream),
|
||||||
),
|
redirect: (context, state) {
|
||||||
GoRoute(path: '/home', builder: (context, state) => const HomeScreen()),
|
final authState = authBloc.state;
|
||||||
GoRoute(
|
final isAuthenticated = authState is AuthAuthenticated;
|
||||||
path: '/messages/invites',
|
final isAuthRoute =
|
||||||
builder: (context, state) => const MessageInviteListScreen(),
|
state.matchedLocation.startsWith('/login') ||
|
||||||
),
|
state.matchedLocation.startsWith('/register');
|
||||||
GoRoute(
|
final isProtected = _protectedRoutes.any(
|
||||||
path: '/messages/invites/:id',
|
(route) => state.matchedLocation.startsWith(route),
|
||||||
builder: (context, state) => const MessageInviteDetailScreen(),
|
);
|
||||||
),
|
|
||||||
GoRoute(
|
if (!isAuthenticated && isProtected) {
|
||||||
path: '/contacts',
|
return '/';
|
||||||
builder: (context, state) => const ContactsScreen(),
|
}
|
||||||
),
|
if (isAuthenticated && isAuthRoute) {
|
||||||
GoRoute(
|
return '/home';
|
||||||
path: '/contacts/add',
|
}
|
||||||
builder: (context, state) => const AddContactScreen(),
|
return null;
|
||||||
),
|
},
|
||||||
GoRoute(
|
routes: [
|
||||||
path: '/calendar/dayweek',
|
GoRoute(path: '/', builder: (context, state) => const LoginEmailScreen()),
|
||||||
builder: (context, state) => const CalendarDayWeekScreen(),
|
GoRoute(
|
||||||
),
|
path: '/login/password',
|
||||||
GoRoute(
|
builder: (context, state) => const LoginPasswordScreen(),
|
||||||
path: '/calendar/month',
|
),
|
||||||
builder: (context, state) => const CalendarMonthScreen(),
|
GoRoute(
|
||||||
),
|
path: '/login/code',
|
||||||
GoRoute(
|
builder: (context, state) => const LoginCodeScreen(),
|
||||||
path: '/calendar/events/:id',
|
),
|
||||||
builder: (context, state) => const CalendarEventDetailScreen(),
|
GoRoute(
|
||||||
),
|
path: '/register',
|
||||||
GoRoute(
|
builder: (context, state) => const RegisterScreen(),
|
||||||
path: '/todo',
|
),
|
||||||
builder: (context, state) => const TodoQuadrantsScreen(),
|
GoRoute(
|
||||||
),
|
path: '/register/step2',
|
||||||
GoRoute(
|
builder: (context, state) => const RegisterStep2Screen(),
|
||||||
path: '/todo/:id',
|
),
|
||||||
builder: (context, state) => const TodoDetailScreen(),
|
GoRoute(path: '/home', builder: (context, state) => const HomeScreen()),
|
||||||
),
|
GoRoute(
|
||||||
GoRoute(
|
path: '/messages/invites',
|
||||||
path: '/settings',
|
builder: (context, state) => const MessageInviteListScreen(),
|
||||||
builder: (context, state) => const SettingsScreen(),
|
),
|
||||||
),
|
GoRoute(
|
||||||
GoRoute(
|
path: '/messages/invites/:id',
|
||||||
path: '/settings/features',
|
builder: (context, state) => const MessageInviteDetailScreen(),
|
||||||
builder: (context, state) => const FeaturesScreen(),
|
),
|
||||||
),
|
GoRoute(
|
||||||
GoRoute(
|
path: '/contacts',
|
||||||
path: '/settings/memory',
|
builder: (context, state) => const ContactsScreen(),
|
||||||
builder: (context, state) => const MemoryScreen(),
|
),
|
||||||
),
|
GoRoute(
|
||||||
GoRoute(
|
path: '/contacts/add',
|
||||||
path: '/settings/account',
|
builder: (context, state) => const AddContactScreen(),
|
||||||
builder: (context, state) => const AccountScreen(),
|
),
|
||||||
),
|
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
@@ -1,21 +1,36 @@
|
|||||||
import 'package:flutter/material.dart';
|
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/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() {
|
void main() async {
|
||||||
runApp(const LinksyApp());
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
|
await configureDependencies();
|
||||||
|
|
||||||
|
final authBloc = sl<AuthBloc>();
|
||||||
|
authBloc.add(AuthStarted());
|
||||||
|
|
||||||
|
runApp(LinksyApp(authBloc: authBloc));
|
||||||
}
|
}
|
||||||
|
|
||||||
class LinksyApp extends StatelessWidget {
|
class LinksyApp extends StatelessWidget {
|
||||||
const LinksyApp({super.key});
|
final AuthBloc authBloc;
|
||||||
|
|
||||||
|
const LinksyApp({super.key, required this.authBloc});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp.router(
|
return BlocProvider<AuthBloc>.value(
|
||||||
title: 'Linksy',
|
value: authBloc,
|
||||||
debugShowCheckedModeBanner: false,
|
child: MaterialApp.router(
|
||||||
theme: AppTheme.light,
|
title: 'Linksy',
|
||||||
routerConfig: appRouter,
|
debugShowCheckedModeBanner: false,
|
||||||
|
theme: AppTheme.light,
|
||||||
|
routerConfig: createAppRouter(authBloc),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,27 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
import 'package:social_app/main.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() {
|
void main() {
|
||||||
|
setUpAll(() {
|
||||||
|
registerFallbackValue(FakeAuthState());
|
||||||
|
});
|
||||||
|
|
||||||
testWidgets('Login screen loads correctly', (WidgetTester tester) async {
|
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('linksy'), findsOneWidget);
|
||||||
expect(find.text('继续'), findsOneWidget);
|
expect(find.text('继续'), findsOneWidget);
|
||||||
expect(find.text('还没有账号?去注册'), findsOneWidget);
|
expect(find.text('还没有账号?去注册'), findsOneWidget);
|
||||||
@@ -13,7 +30,13 @@ void main() {
|
|||||||
testWidgets('Main content is vertically centered above footer', (
|
testWidgets('Main content is vertically centered above footer', (
|
||||||
WidgetTester tester,
|
WidgetTester tester,
|
||||||
) async {
|
) 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 safeAreaRect = tester.getRect(find.byType(SafeArea));
|
||||||
final mainRect = tester.getRect(
|
final mainRect = tester.getRect(
|
||||||
|
|||||||
Reference in New Issue
Block a user