-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b52ca26
commit 6fadbd1
Showing
2 changed files
with
39 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,53 @@ | ||
import 'package:evlve/app/router/router_listenable.dart'; | ||
import 'package:evlve/app/router/routes.dart'; | ||
import 'package:evlve/modules/auth/auth.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
|
||
part 'router.g.dart'; | ||
|
||
@riverpod | ||
Raw<GoRouter> router(RouterRef ref) { | ||
final notifier = ref.watch(routerListenableProvider.notifier); | ||
GoRouter router(RouterRef ref) { | ||
final authState = ValueNotifier<AsyncValue<AuthState>>(const AsyncLoading()); | ||
ref | ||
..onDispose(authState.dispose) | ||
..listen(authControllerProvider, (_, value) { | ||
authState.value = value; | ||
}); | ||
|
||
final router = GoRouter( | ||
routes: $appRoutes, | ||
navigatorKey: rootKey, | ||
debugLogDiagnostics: true, | ||
redirect: notifier.redirect, | ||
refreshListenable: notifier, | ||
refreshListenable: authState, | ||
initialLocation: SplashRoute.path, | ||
debugLogDiagnostics: true, | ||
routes: $appRoutes, | ||
redirect: (context, state) { | ||
if (authState.value.unwrapPrevious().hasError) { | ||
return const LoginRoute().location; | ||
} | ||
if (authState.value.isLoading || !authState.value.hasValue) { | ||
return const SplashRoute().location; | ||
} | ||
final auth = authState.value.requireValue; | ||
final isSplash = state.uri.path == const SplashRoute().location; | ||
if (isSplash) { | ||
return auth.map( | ||
loggedIn: (_) { | ||
final currentRoute = state.uri.toString(); | ||
final isLoggingIn = | ||
currentRoute.startsWith(const LoginRoute().location); | ||
return isLoggingIn ? const ScheduleRoute().location : null; | ||
}, | ||
loggedOut: (_) => const LoginRoute().location, | ||
requireOtp: (_) => const OtpRoute().location, | ||
); | ||
} | ||
|
||
return null; | ||
}, | ||
); | ||
|
||
ref.onDispose(router.dispose); | ||
|
||
return router; | ||
} |
This file was deleted.
Oops, something went wrong.