Flutter

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 ParseUseremail 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:

  1. true - the user confirmed his or her email address by clicking on the link Parse emailed them. ParseUsers can never have a true value when the user account is first created.
  2. false - at the time the ParseUser object was last fetched, the user had not confirmed his or her email address. If emailVerified is false. If emailVerified is false, consider calling getUpdatedUser() on the ParseUser.

  3. missing - the ParseUser was created when email verification was off or the ParseUser 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:

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.

  1. Go to your App at Back4App Website and click on Server Settings.
  2. Find the Verification emails card and click on Settings`.

    email-verification

  3. Click on Verify User Email and Prevent login if the email is not verified.

    email-verification-prevent-login

  4. Optional: Fill the empty fields and modify the ones that have already been filled based on your preferences.
  5. 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
22
23
24
  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) {
    //User logout. force login with email verification
      await user.logout();

      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.

flutter-login-back4app

Perform the registration process, clicking in button Sign Up.

flutter_user_verification_email-01

After SignUp we will receieve an email like this:

email-verification-step4

After click in link to verify the email, the property will be setted to true in Parse Dashboard:

email-verification-step4-attribute

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.

flutter_user_verification_email-02

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!