Comments (7)
Hey @edwardaux You can go about achieving that using route guards.
Here are two approaches you could use
1- Let your loginScreen route return true if users successfully login.
then inside of your AuthGuard check if user is already logged in, continue to route /b otherwise navigate to the login page, your login page should return true if the auth process is a success.
class AuthGuard extends RouteGuard {
Future<bool> canNavigate(ExtendedNavigatorState navigator, String routeName,
Object arguments) async {
if (isLoggedin) {
return true;
}
return await navigator.pushNamed<bool>(Routes.loginScreen);
}
}
inside of your login page you can pop with a result using
ExtendedNavigator.of(context).pop(true|false)
2- Pass your guarded route name and args to the loginScreen so you can later redirect to the desired destination in case of a successful login.
class AuthGuard extends RouteGuard {
Future<bool> canNavigate(ExtendedNavigatorState navigator, String routeName,
Object arguments) async {
if (isLoggedin) {
return true;
}else{
navigator.pushNamed<bool>(Routes.loginScreen, arguments:
LoginScreenArguments(
redirectRouteName: routeName,
redirectRouteArgs : arguments),
);
return false;
}
}
Then inside your loginScreen you can redirect users to any guarded route after a successful login process
ExtendedNavigator.of(context).pushNamed(redirectRouteName, arguments: redirectRouteArgs);
Let me know if you have more questions.
from auto_route_library.
Hey, @rahulnakre well you could directly check your SharedPreferences for a token or if you're using an auth service or something you could inject it in the AuthGuard constructor
class AuthGuard extends RouteGuard {
Future<bool> canNavigate(ExtendedNavigatorState navigator, String routeName,
Object arguments) async {
final prefs = await SharedPrefrences.instance();
final isLoggedIn = prefs.getString('token') != null
if (isLoggedIn) {
return true;
}
return await navigator.pushNamed<bool>(Routes.loginScreen);
}
}
or
class AuthGuard extends RouteGuard {
final MyAuthService authService;
AuthGuard(this.authService);
Future<bool> canNavigate(ExtendedNavigatorState navigator, String routeName,
Object arguments) async {
if (authService.isLoggedIn()) {
return true;
}
return await navigator.pushNamed<bool>(Routes.loginScreen);
}
}
then inside in ExtendedNavigator
ExtendedNavigator(
router:Router(),
guards:[ AuthGuard(MyAuthService()),]
),
from auto_route_library.
In case anyone is playing along at home, I tried both approaches above.
I initially used the second one because my LoginScreen already had some logic in to do the redirect. However, there are two downsides to this approach:
- It relies on LoginScreen to call navigator.pushNamed() - which is problematic if the original caller wanted to use pushReplacementNamed() to transition
- It pushes redirection logic and state into the LoginScreen
Going with the first option works better (in my opinion) because now the callerโs preference for pushing is maintained, and the redirection logic is purely contained in the guard.
Note that the last check in the guard needs to be null-aware (because the LoginScreen returns with a null if it was presented modally and dismissed with by tapping in the X button)
from auto_route_library.
Ah, thank you so much. I didn't realise I could also change the destination of the route inside the guard... perfect ๐ฏ
from auto_route_library.
class AuthGuard extends RouteGuard {
Future<bool> canNavigate(ExtendedNavigatorState navigator, String routeName,
Object arguments) async {
if (isLoggedin) {
return true;
}
return await navigator.pushNamed<bool>(Routes.loginScreen);
}
}
Where does isLoggedIn
come from? How do I pass that variable from my widget to my AuthGuard
? Thanks!
from auto_route_library.
Thanks for the help!
from auto_route_library.
how can I set a splash screen while I check for authentication in AuthGuard?
from auto_route_library.
Related Issues (20)
- Erro de null ao navegar para um filho de um AutoTabsRouter.pageView HOT 1
- How to get redirectFrom for a guard redirection HOT 2
- Deeplink builder does not working
- browser refresh issue HOT 1
- App crashes when trying to navigate from modal, which is animating. HOT 1
- Udpate documentation for Autoroute Declaritive Routing HOT 1
- Redirect is not working in `AutoRouteGuard` HOT 8
- Navigate Function causes clearing for path params HOT 1
- AutoLeadingButton custom semantic label
- onNavigation guard causes Stack Overflow
- In NESTED NAVIGATION the pages overlap in the transition HOT 1
- infinite loop on context.router.pop()
- AutoTabsRouter testecase fail. HOT 4
- Initial page created when homeIndex is set differently
- add shallow routing
- Functionality to control route changes
- Functionality to control route changes HOT 4
- Unhandled Exception: type 'AutoRoutePage<dynamic>' is not a subtype of type 'AutoRoutePage<Object>' in type HOT 2
- Inherited params not working and wrong navigation HOT 1
- Generator uses absolute paths which generates an error in my app
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from auto_route_library.