diff --git a/app/lib/router.dart b/app/lib/router.dart index cebfefd..9048991 100644 --- a/app/lib/router.dart +++ b/app/lib/router.dart @@ -26,6 +26,12 @@ import 'features/shell/app_shell.dart'; import 'features/tax/tax_page.dart'; import 'features/transactions/transactions_page.dart'; +/// Where to go after signing in: the page the user was on, if `from` is an in-app path. +String _returnTo(String? from) { + final inApp = from != null && from.startsWith('/') && !from.startsWith('//'); + return inApp && !from.startsWith('/login') ? from : '/'; +} + final routerProvider = Provider((ref) { final auth = ValueNotifier(ref.read(authControllerProvider)); ref.listen(authControllerProvider, (_, next) => auth.value = next); @@ -38,9 +44,19 @@ final routerProvider = Provider((ref) { final status = auth.value.status; final atLogin = state.matchedLocation == '/login'; return switch (status) { - AuthStatus.unknown => atLogin ? null : '/login', + // A reload starts here while the stored session is still being restored: the address + // is kept in `from` so signing back in lands on it instead of on the home page. A + // deliberate sign-out is `signedOut` from the start and carries no `from`. + AuthStatus.unknown => + atLogin + ? null + : Uri( + path: '/login', + queryParameters: {'from': state.uri.toString()}, + ).toString(), AuthStatus.signedOut => atLogin ? null : '/login', - AuthStatus.signedIn => atLogin ? '/' : null, + AuthStatus.signedIn => + atLogin ? _returnTo(state.uri.queryParameters['from']) : null, }; }, routes: [