Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: How to handle screens that require authentication?

I'm building a Flutter app where some screens can be shown to anonymous users, and other screens require a user to be logged in.

For the authenticated screens, they should automatically navigate to (push) a login screen if the user is not logged in. A user session can expire at any time and if the user is viewing one of these authenticated screens then the login screen should be shown immediately at that time.

In Flutter, how can I achieve this notion of authenticated screens that automatically navigate to/from a login screen when the user is not authenticated?

like image 772
SuperDeclarative Avatar asked Dec 13 '17 21:12

SuperDeclarative


People also ask

How do you code a login screen in Flutter?

Flutter Login ScreenFirst there is a widget for the company/organization/app name. Then about the screen itself, Sign in. Now, we have two text fields, user name and password, to get login/sign-in credentials from user. Then we have a TextButton widget for the Forgot Password.


1 Answers

Currently, there's nothing that flutter do with Authentification and Authentified routes. The problem is that dart:mirror is disabled, which prevents from doing a more automated solution.

You could try to :

  • Put Anonymous routes in MaterialApp's routes property
  • Put Authentified routes in MaterialApp's onGenerateRoute property

And make sure inside onGenerateRoute that the user is logged. If he is, build that route. If not, build the Login route with the original destination passed as parameter (to later redirect to that page)

A code generator may be a good solution too ; although more complex.

like image 152
Rémi Rousselet Avatar answered Oct 17 '22 03:10

Rémi Rousselet