Login
Description
This module provides screens and functionality to add Login, SignUp and password recovery to your Flutter app.
You can also extend the module to support new login providers.
Supported Providers
At this moment, the module support the following providers:
- Email and password
- Google (How To Setup Google Sign In)
- X / Twitter (How To Setup X/Twitter)
You can enable or disable providers in the configuration phase.
Dependencies
Base module
Email module
- email_validator: ^2.1.17
- http: ^1.2.0
Google module
- google_sign_in: ^6.2.1
Twitter Module
- twitter_login: ^4.4.2
Get started
- Download the module from our shop and extrat it somewhere. You will have 6 folders:
- login_auth_email
- login_auth_google
- login_auth_module
- login_auth_x
- login_module
- example
-
Create a folder called
vendors
in your project -
Copy the modules you want to use to the
vendors/
folder. You will need at leastlogin_module
andlogin_auth_module
. Other Auth providers are optional, but you need at least one. -
Add the dependencies in your’s app
pubspec.yml
:
How to configure the module
This module have two dependencies:
- An
Auth
instance that needs to be registered. - A Theme extension that needs to be provided in the theme definition
Providing an Auth instance
The module provides a AuthRegister
class so you can provide the Auth class.
The first thing you need to do import the main module and the auth providers you want to use:
Then, register the instance:
Now you can call runApp
to start your app.
Customize module via Theme extension
A theme extension is a tool that Flutter provides to add extra information to a
ThemeData
so you have it available everywhere. We use a theme extension so you
can configure all the visual customizations in just one place.
To provide an extension you should crete an instance of the provided LoginThemeExtension
and add it to the current theme:
This will set the background image of all the login flow screens to the same image.
Flutter Router and Current User
You can read the current logged in user, if any, by getting the instance of Auth
and look for changes.
This is very useful if you also use it in convination with GoRouter
, so you can redirect the user
to the login flow if needed or to the home if already it’s logged in as show next:
The refreshListenable
will make the router to reevaluate the current route if something change in
the log in status.
The redirect
will ensure that if the user is not logged in, the user is redirected and can’t
see other screens. This is special important for Flutter for web.
Once you have your router defined, you can just use it in the app:
Using Flutter Bloc
If you want to use Flutter Bloc with the login module you can do it with a custom bloc to handle the login.
Login State
First, create the state class so the UI can listen for state changed in the login state:
Login Event
And you will also need a bloc event class to handle login events:
Login Block
Last, create a new bloc class and add the logic to listen to the auth currentUser
stream:
When the LoginBloc
it’s created it will start listening for the value in the currentUser
stream of the
Auth
instance. It will inject a new event directly into the bloc so we can later emit the matching LoginState
.
Listen to LoginState changes
Now that we have our bloc in place, you only need to use it in the root of your application:
Now, the root of the app will change if the user is logged in or not and we will show the right app state to the user.
If you have more bloc providers, just add them to the MultiBlocProvider
.