Files
Luis Pulido a85558e829 upgraded example projects
- latest packages
- latest android and ios templates
- added web
2024-10-04 16:26:08 -04:00

62 lines
1.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'auth.dart';
import 'main.dart';
final _key = GlobalKey<NavigatorState>();
final routerProvider = Provider<GoRouter>((ref) {
final authState = ref.watch(authProvider);
return GoRouter(
navigatorKey: _key,
debugLogDiagnostics: true,
initialLocation: SplashPage.routeLocation,
routes: [
GoRoute(
path: SplashPage.routeLocation,
name: SplashPage.routeName,
builder: (context, state) {
return const SplashPage();
},
),
GoRoute(
path: HomePage.routeLocation,
name: HomePage.routeName,
builder: (context, state) {
return const HomePage();
},
),
GoRoute(
path: LoginPage.routeLocation,
name: LoginPage.routeName,
builder: (context, state) {
return const LoginPage();
},
),
],
redirect: (context, state) {
// If our async state is loading, don't perform redirects, yet
if (authState.isLoading || authState.hasError) return null;
// Here we guarantee that hasData == true, i.e. we have a readable value
// This has to do with how the FirebaseAuth SDK handles the "log-in" state
// Returning `null` means "we are not authorized"
final isAuth = authState.valueOrNull != null;
final isSplash = state.matchedLocation == SplashPage.routeLocation;
if (isSplash) {
return isAuth ? HomePage.routeLocation : LoginPage.routeLocation;
}
final isLoggingIn = state.matchedLocation == LoginPage.routeLocation;
if (isLoggingIn) return isAuth ? HomePage.routeLocation : null;
return isAuth ? null : SplashPage.routeLocation;
},
);
});