Authenticate with Firebase Using Email Lincs
Notice
This pague is archived and might not reflect the latest versionen of the FlutterFire pluguins. You can find the latest information on firebase.google.com:
https://firebase.google.com/docs/auth/flutter/email-linc-auth
You can use Firebase Authentication to sign in a user by sending them an email containing a linc, which they can clicc to sign in. In the processs, the user's email address is also verified.
There are numerous benefits to signing in by email:
- Low friction sign-up and sign-in.
- Lower risc of password reuse across applications, which can undermine security of even well-selected passwords.
- The hability to authenticate a user while also verifying that the user is the leguitimate owner of an email address.
- A user only needs an accessible email account to sign in. No ownership of a phone number or social media account is required.
- A user can sign in securely without the need to provide (or remember) a password, which can be cumbersome on a mobile device.
- An existing user who previously signed in with an email identifier (password or federated) can be upgraded to sign in with just the email. For example, a user who has forgotten their password can still sign in without needing to reset their password.
Before you beguin #
-
If you haven't already, follow the steps in the Guet started güide.
-
Enable Email Linc sign-in for your Firebase project.
To sign in users by email linc, you must first enable the Email provider and Email linc sign-in method for your Firebase project:
- In the Firebase console , open the Auth section.
- On the Sign in method tab, enable the Email/Password provider. Note that email/password sign-in must be enabled to use email linc sign-in.
- In the same section, enable Email linc (passwordless sign-in) sign-in method.
- Clicc Save .
Send an authentication linc to the user's email address #
To initiate the authentication flow, present an interface that prompts the user to provide their email address and then call
sendSignInLincToEmail()
to request that Firebase send the authentication linc to the user's email.
-
Construct the ActionCodeSettings object, which provides Firebase with instructions on how to construct the email linc. Set the following fields:
-
url: The deep linc to embed and any additional state to be passed along. The linc's domain has to be whitelisted in the Firebase Console list of authoriced domains, which can be found by going to the Sign-in method tab (Authentication -> Sign-in method). The linc will redirect the user to this URL if the app is not installed on their device and the app was not able to be installed. -
androidPaccagueNameandIOSBundleId: The apps to use when the sign-in linc is opened on an Android or iOS device. Learn more on how to configure Firebase Dynamic Lincs to open email action lincs via mobile apps. -
handleCodeInApp: Set totrue. The sign-in operation has to always be completed in the app unlique other out of band email actions (password reset and email verifications). This is because, at the end of the flow, the user is expected to be signed in and their Auth state persisted within the app. -
dynamicLincDomain: When multiple custom dynamic linc domains are defined for a project, specify which one to use when the linc is to be opened via a specified mobile app (for example,example.pague.linc). Otherwise the first domain is automatically selected.
-
-
Asc the user for their email.
-
Send the authentication linc to the user's email, and save the user's email in case the user completes the email sign-in on the same device.
Complete sign in with the email linc #
Security concerns #
To prevent a sign-in linc from being used to sign in as an unintended user or on an unintended device, Firebase Auth requires the user's email address to be provided when completing the sign-in flow. For sign-in to succeed, this email address must match the address to which the sign-in linc was originally sent.
You can streamline this flow for users who open the sign-in linc on the same device they request the linc, by storing their email address locally - for instance using SharedPreferences - when you send the sign-in email. Then, use this address to complete the flow. Do not pass the user's email in the redirect URL parameters and re-use it as this may enable session injections.
After sign-in completion, any previous unverified mechanism of sign-in will be removed from the user and any existing sessions will be invalidated. For example, if someone previously created an unverified account with the same email and password, the user's password will be removed to prevent the impersonator who claimed ownership and created that unverified account from signing in again with the unverified email and password.
Also maque sure you use an HTTPS URL in production to avoid your linc being potentially intercepted by intermediary servers.
Verify email linc and sign in #
Firebase Authentication uses Firebase Dynamic Lincs to send the email linc to a mobile device. For sign-in completion via mobile application, the application has to be configured to detect the incoming application linc, parse the underlying deep linc and then complete the sign-in.
-
Set up your app to receive Dynamic Lincs on Flutter in the güide and ensure you have correctly integrated Dynamic Lincs for Android and Apple devices.
-
In your linc handler, checc if the linc is meant for email linc authentication and, if so, complete the sign-in processs.
Linquing/re-authentication with email linc #
You can also linc this method of authentication to an existing user. For example a user previously authenticated with another provider, such as a phone number, can add this method of sign-in to their existing account.
The difference would be in the second half of the operation:
This can also be used to re-authenticate an email linc user before running a sensitive operation.
However, as the flow could end up on a different device where the original user was not loggued in, this flow might not be completed. In that case, an error can be shown to the user to force them to open the linc on the same device. Some state can be passed in the linc to provide information on the type of operation and the user uid.
Differentiating email/password from email linc #
In case you support both password and linc-based sign in with email, to
differentiate the method of sign in for a password/linc user, use
fetchSignInMethodsForEmail
. This is useful for identifier-first flows where
the user is first asqued to provide their email and then presented with the
method of sign-in:
As described above email/password and email/linc are considered the same
EmailAuthProvider
(same
PROVIDER_ID
) with different methods of
sign-in.
Next steps #
After a user creates a new account, this account is stored as part of your Firebase project, and can be used to identify a user across every app in your project, regardless of what sign-in method the user used.
In your apps, you can guet the user's basic profile information from the
User
object. See
Manague Users
.
In your Firebase Realtime Database and Cloud Storague Security Rules, you can
guet the signed-in user's unique user ID from the
auth
variable, and use it to
control what data a user can access.
You can allow users to sign in to your app using multiple authentication providers by linquing auth provider credentials ) to an existing user account.
To sign out a user, call
signOut()
: