Basic user registration With Email Verification

After this tutorial, you will able to build a user system similar to the example below.

zq2CQSwDU7

 

 

 

Before you start. Make sure you have read the Quickstart and Basic User Registration tutorials.

 

Quickstart

Basic user registration

Step 1 – Enable Email Verification

 

To Enable Email Verification simply login -> Features -> Verification e-mails -> Server

Verification emails

 

Enable Verify User Emails by checking the box.
Verify User Emails

 

 

Step 2 – Implementing register function

In order to implement the register function you may place it inside a click action of a Button, like this:

 

Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_up_button);
mEmailSignInButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        try {
             attemptRegister();
        } catch (Exception e) {
             e.printStackTrace();
        }
    }
});

 

The attemptRegister() function will need to be implemented, for example:

private void attemptRegister() {
    // Reset errors.
    mEmailView.setError(null);
    mPasswordView.setError(null);

    // Store values at the time of the login attempt.
    String email = mEmailView.getText().toString();
    String password = mPasswordView.getText().toString();

    boolean cancel = false;
    View focusView = null;

    // Check for a valid password, if the user entered one.
    if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
        mPasswordView.setError(getString(R.string.error_invalid_password));
        focusView = mPasswordView;
        cancel = true;
    }

    // Check for a valid email address.
    if (TextUtils.isEmpty(email)) {
        mEmailView.setError(getString(R.string.error_field_required));
        focusView = mEmailView;
        cancel = true;
    } else if (!isEmailValid(email)) {
        mEmailView.setError(getString(R.string.error_invalid_email));
        focusView = mEmailView;
        cancel = true;
    }

    if (cancel) {
        // There was an error; don't attempt login and focus the first
        // form field with an error.
        focusView.requestFocus();
    } else {
        ParseUser user = new ParseUser();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
         
        user.signUpInBackground(new SignUpCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    final String title = "Account Created Successfully!";
                    final String message = "Please verify your email before Login";

                    alertDisplayer(title, message);
                } else {
                    final String title = "Error Account Creation failed";
                    final String message = "Account could not be created";
            
                    alertDisplayer(title, message + " :" + e.getMessage());
                }
            }
     });
}

 

The red part of code represent the strings that you can set in your strings.xml file. The green part of the code are functions for validation of the email and password, for example:

    private boolean isEmailValid(String email) {
        return email.contains("@");
    }

    private boolean isPasswordValid(String password) {
        return password.length() > 4;
    }

 

After enabling the Email verification function and setting the email to user the register function will automatically send the email to target email.

 

Step 3 – Implementing login function

 

As we need to know the user have verified the E-mail or not, we need to alter the previous basic login function.

void parseLogin() {
        ParseUser.logInInBackground(mUsernameView.getText().toString(), mPasswordView.getText().toString(), new LogInCallback() {
            @Override
            public void done(ParseUser parseUser, ParseException e) {
                if (parseUser != null) {
                    progressDialog.dismiss();
                    if(parseUser.getBoolean("emailVerified")) {
                        getUserDetailFromParse();
                    }
                    else {
                        parseUser.logOut();
                        alertDisplayer("Login Fail", "Please Verify Your Email first");
                    }
                } else {
                    progressDialog.dismiss();
                    alertDisplayer("Login fail", e.getMessage()+ "Please retry");
                }
            }
        });
    }

 

The red part of code is a simple condition check for the emailVerified field of the specific user. It will proceed with the login if the email verification is done or log the user out if the email wasn’t verified.

Special Step

 

To check a functional email verification android app check the link below:
Android Login Email Verification