Flutter: How to handle screens requiring authentication?

I am creating a Flutter application where some screens may be displayed to anonymous users, while other screens require the user to be logged in.

For authenticated screens, they should automatically go to the (click) login screen if the user does not log in. A user session may expire at any time, and if the user views one of these authentication screens, then the login screen should be displayed immediately at that time.

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

+4
source share
1 answer

Currently nothing is happening with Authentification and Authentified. The problem is that dart: mirror is disabled, which prevents the implementation of a more automated solution.

You can try:

  • Put anonymous routes into MaterialApp routesproperty
  • Put authenticated routes in the MaterialApp onGenerateRouteproperty

And make sure the onGenerateRouteuser is logged in internally . If he is, build this route. If not, build a route Loginwith the original destination passed as a parameter (for subsequent redirection to this page)

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

+1
source

Source: https://habr.com/ru/post/1690742/


All Articles