User email verification for Flutter
Introduction
Enabling email verification in an application’s settings allows the application to reserve part of its experience for users with confirmed email addresses.
Email verification adds the emailVerified
key to the ParseUser
object. When a ParseUser
email is set or modified, emailVerified
is set to false
.
Parse then emails the user a link which will set emailVerified
to true
.
There are three emailVerified
states to consider:
- true - the user confirmed his or her email address by clicking on the link Parse emailed them.
ParseUsers
can never have atrue
value when the user account is first created. -
false - at the time the
ParseUser
object was last fetched, the user had not confirmed his or her email address. IfemailVerified
isfalse
. If emailVerified is false, consider callinggetUpdatedUser()
on the ParseUser. - missing - the
ParseUser
was created when email verification was off or theParseUser
does not have an email.
In this guide, you will learn how to set up a user email verification process to a user registration feature (Sign Up).
You will create an app that includes user registration with email verification using Parse Server core features through Back4App.
You will use the same method you used to implement the user registration, but instead of redirecting the user to a logged screen, you will ask the user to verify their email to log in.
Goal
Build a User verification email process feature using Parse for a Flutter App.
Prerequisites
To complete this tutorial, you will need:
- Flutter version 2.2.x or later
- Android Studio or VS Code installed (with Plugins Dart and Flutter)
- The Flutter app created in previous guide.
- Note: Follow the How to implement user password reset
- Complete the previous guide so you can have a better understanding of the
ParseUser
class.- A device (or virtual device) running Android or iOS.
Understanding Email verification function
To better understand Email verification function, we will continue the development of the application started in the previous guide and implement the function.
We won’t explain the Flutter application code once this guide’s primary focus is using the Flutter with Parse. Following the next steps, you will build a Login e Logout App at Back4App Database.
Let’s get started!
In the following steps, you will be able to build a Email verification function in App.
Step 1 - Enable Email Verification
Let’s now enable the email verification on Back4App Dashboard. The email verification page has two properties: Verify User Emails
and Prevent login if the email is not verified
.
If you enable only the Verify User Emails option, the user will receive the verification email but will be able to log in and use the application normally. If you also enable the Prevent login if email is not verified
option, the user will only log in after concluding the email verification process.
- Go to your App at Back4App Website and click on
Server Settings
. - Find the
Verification emails card and click on
Settings`. - Click on
Verify User Email
andPrevent login if the email is not verified
. - Optional: Fill the empty fields and modify the ones that have already been filled based on your preferences.
- Click on the
SAVE
button.
Step 2 - Update the Login/Logout/Reset Password App
Open Flutter project from the previous guide How to add user reset password to a Flutter App.
Search for the function doUserRegistration
in the file main.dart
.
After call function user.signUp();
, call the user.logout()
function, to ensure that the user does not log in until the email is confirmed.
Update the message informing the user to check the mailbox e redirect the user to Home Screen.
Replace the code inside doUserRegistration
with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void doUserRegistration() async {
final username = controllerUsername.text.trim();
final email = controllerEmail.text.trim();
final password = controllerPassword.text.trim();
final user = ParseUser.createUser(username, password, email);
var response = await user.signUp();
if (response.success) {
Message.showSuccess(
context: context,
message: 'User was successfully created! Please verify your email before Login',
onPressed: () async {
Navigator.pop(context);
});
} else {
Message.showError(context: context, message: response.error!.message);
}
}
Note: The code for SignUp function has been explained previously.
Step 3 - Test Sign Up
To test it, click on the Run
button in Android Studio/VSCode.
Perform the registration process, clicking in button Sign Up
.
After SignUp we will receieve an email like this:
After click in link to verify the email, the property will be setted to true in Parse Dashboard:
Step 4 - Log in
To implement the Log In with Email Verification, you have just to implement a Parse User Logins just as described on User LogIn guide.
If you have enabled the ‘Prevent login if email is not verified’ option in Step 2, you will get the following error if you try to login without verifying your email.
It’s done!
At this stage, you can Log in, Sign Up or Log out of your app using email verification with Parse Server core features through Back4App!