Sub topic 10
Sending Deep Links Through Login
Assume you know basic Dart, Flutter widgets, and go_router. You should also understand URI paths and query parameters. This chapter focuses on protecting a deep-linked screen while preserving the screen the user originally requested.
Why a guard must preserve the destination
A user may open this link while signed out:
https://example.com/products/42?ref=emailThe router should send the user to login. After successful authentication, it should return the user to /products/42?ref=email, not simply show the home screen.
A router redirect is a function that chooses another location before a route is displayed. With go_router, refreshListenable makes the router reevaluate its redirect when authentication changes.
Preserve and validate the original route
The following example uses a ChangeNotifier for authentication. The login button changes the state, which causes go_router to run the redirect again.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
class AuthState extends ChangeNotifier {
bool isLoggedIn = false;
void signIn() {
isLoggedIn = true;
notifyListeners();
}
}
final auth = AuthState();
String safeInternalPath(String? value) {
if (value == null || value.isEmpty) return '/';
final uri = Uri.tryParse(value);
final isInternal = uri != null &&
!uri.hasScheme &&
!uri.hasAuthority &&
value.startsWith('/') &&
!value.startsWith('//');
return isInternal ? uri.toString() : '/';
}
final router = GoRouter(
refreshListenable: auth,
redirect: (context, state) {
final location = state.matchedLocation;
final isLoginPage = location == '/login';
if (!auth.isLoggedIn && !isLoginPage) {
final from = Uri.encodeComponent(state.uri.toString());
return '/login?from=$from';
}
if (auth.isLoggedIn && isLoginPage) {
final destination =
safeInternalPath(state.uri.queryParameters['from']);
return destination;
}
return null;
},
routes: [
GoRoute(
path: '/',
builder: (context, state) =>
const Scaffold(body: Center(child: Text('Home'))),
),
GoRoute(
path: '/login',
builder: (context, state) => Scaffold(
body: Center(
child: ElevatedButton(
onPressed: auth.signIn,
child: const Text('Sign in'),
),
),
),
),
GoRoute(
path: '/products/:id',
builder: (context, state) => Scaffold(
body: Center(
child: Text('Product ${state.pathParameters['id']}'),
),
),
),
],
);
void main() {
runApp(MaterialApp.router(routerConfig: router));
}state.uri.toString() preserves both path and query values. Uri.encodeComponent makes the entire destination safe to place inside the from query parameter. When login succeeds, refreshListenable triggers the redirect, which reads and validates from.
The isLoginPage check prevents a redirect loop. A signed-out user can remain on /login, while a signed-in user is sent to the saved internal route. If the value is missing or unsafe, the fallback is /.
The redirect flow
Keep authentication redirects limited to navigation. Do not use the saved URL to grant permissions. The destination may identify a resource, but the server and application must still check whether the signed-in user can access it.
Exercise
A user opens /products/42?ref=email while signed out. Which value should the login route preserve?
Show answer
Preserve the complete internal URI, including its query string: /products/42?ref=email. Encode it before placing it in the from query parameter, then validate it before navigating.