fix(app): перезагрузка страницы возвращает на открытый экран, а не на главную
Пока сессия восстанавливается, адрес запоминается в /login?from=…, после входа роутер возвращает на него. Принимаются только пути внутри приложения.
This commit is contained in:
+18
-2
@@ -26,6 +26,12 @@ import 'features/shell/app_shell.dart';
|
|||||||
import 'features/tax/tax_page.dart';
|
import 'features/tax/tax_page.dart';
|
||||||
import 'features/transactions/transactions_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<GoRouter>((ref) {
|
final routerProvider = Provider<GoRouter>((ref) {
|
||||||
final auth = ValueNotifier<AuthState>(ref.read(authControllerProvider));
|
final auth = ValueNotifier<AuthState>(ref.read(authControllerProvider));
|
||||||
ref.listen(authControllerProvider, (_, next) => auth.value = next);
|
ref.listen(authControllerProvider, (_, next) => auth.value = next);
|
||||||
@@ -38,9 +44,19 @@ final routerProvider = Provider<GoRouter>((ref) {
|
|||||||
final status = auth.value.status;
|
final status = auth.value.status;
|
||||||
final atLogin = state.matchedLocation == '/login';
|
final atLogin = state.matchedLocation == '/login';
|
||||||
return switch (status) {
|
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.signedOut => atLogin ? null : '/login',
|
||||||
AuthStatus.signedIn => atLogin ? '/' : null,
|
AuthStatus.signedIn =>
|
||||||
|
atLogin ? _returnTo(state.uri.queryParameters['from']) : null,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
routes: [
|
routes: [
|
||||||
|
|||||||
Reference in New Issue
Block a user