Android

Login and User registration tutorial

Introduction

This section explains how you can create an app with a simple user registration using Parse Server core features through Back4App.

This is how it will look like:

User Registration App

At any time, you can access the complete Android Project built with this tutorial at our GitHub repository.

Prerequisites

To complete this tutorial, you need:

Step 1 - Set up

  1. At the beginning of each Parse activity, import the following:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    // Front End Dependencies
    import android.app.AlertDialog;
    import android.app.ProgressDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.Toast;
    // Parse Dependencies
    import com.parse.Parse;
    import com.parse.ParseException;
    import com.parse.ParseUser;
    
  2. Then, you need to initialize Parse, so in the onCreate method, right before setContentView, use the following in each activity:
    1
    Parse.initialize(this);
    

Step 2 - Sign Up

Signing up basically involves saving a new object of class ParseUser, shown as “User” in your app Dashboard, and setting at least two of its pre-defined attributes: username and password. In order to set these attributes, two specific methods of this class are used: ParseUser.setUsername() and ParseUser.setPassword().

The method used for saving the new user on the Dashboard is ParseUser.signUpInBackground(), which may come together with a callback function.

Note: Objects of this special class are not saved on the Dashboard with ParseObject.save() method.

To make SignUpActivity work, follow these steps:

  1. Import into your SignUpActivity, in addition to the dependencies imported in Step 1:
    1
    import com.parse.SignUpCallback;
    
  2. To implement user registration, simply use the following code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    ParseUser user = new ParseUser();
    // Set the user's username and password, which can be obtained by a forms
    user.setUsername(<Insert Username Here>);
    user.setPassword(<Insert User Password Here>);
    user.signUpInBackground(new SignUpCallback() {
     @Override
     public void done(ParseException e) {
         if (e == null) {
             alertDisplayer("Sucessful Sign Up!","Welcome" + <Insert Username Here> + "!");
         } else {
             ParseUser.logOut();
             Toast.makeText(SignUpActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
         }
     }
    });
    

    In the example project, this code is placed inside a SIGN UP button callback.
    Also, username and password are caught using Edit Texts.

  3. It’s interesting to add an additional method to display Alert Dialogs and make the process look more professional. The method below do this:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    private void alertDisplayer(String title,String message){
        AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this)
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                        // don't forget to change the line below with the names of your Activities
                        Intent intent = new Intent(SignUpActivity.this, LogoutActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                });
        AlertDialog ok = builder.create();
        ok.show();
    }
    

Step 3 - Log in

Logging in creates a Session object, which points to the User logged in. If login is successful, ParseUser.getCurrentUser() returns a User object, and a Session object is created in the Dashboard. Otherwise, if the target username does not exist, or the password is wrong, it returns null.

The method used to perform the login action is ParseUser.logInInBackground(), which requires as many arguments as the strings of username and password, and may call a callback function.

Note: After signing up, login is performed automatically.

To make LoginActivity work, follow these steps:

  1. Import into your LoginActivity, in addition to the dependencies imported in the Step 1:
    1
    import com.parse.LogInCallback;
    
  2. To implement user login function, simply use the code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    ParseUser.logInInBackground(<Insert Username Here>, <Insert User Password Here>, new LogInCallback() {
     @Override
     public void done(ParseUser parseUser, ParseException e) {
         if (parseUser != null) {
             alertDisplayer("Sucessful Login","Welcome back" + <Insert Username Here> + "!");
    
         } else {
             ParseUser.logOut();
             Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
         }
     }
    });
    

    In the example project, this code is placed inside a LOG IN button callback.
    Also, username and password are caught using Edit Texts.

    The method alertDisplayer is the same that you added in the SignUpActivity, don’t forget to change its Intent arguments though.

Step 4 - Log Out

Logging out deletes the active Session object for the logged User. The method used to perform log out is ParseUser.logOut().

To implement user log out, simply use the code below, in the LogoutActivity:

1
2
3
// logging out of Parse
ParseUser.logOut();
alertDisplayer("So, you're going...", "Ok...Bye-bye then");

In the example project, this code is placed inside a LOG OUT button callback.

The method alertDisplayer is the same that you added in the LoginActivity and SignUpActivity, don’t forget to change its Intent arguments though.

Step 5 - Test your app

  1. Run your app and create a couple of users, also try logging in again after registering them.
  2. Login at Back4App Website.
  3. Find your app and click on Dashboard > Core > Browser > User.

At this point, you should see your users as displayed below:

Parse Dashboard

Note: Using the codes displayed above, every time you log in with a user, a Session is opened in your Dashboard, but when the user logs out that particular Session ends. Also, whenever an unsuccessful login or sign up attempt occurs, the Session opened in Parse Server Dashboard is deleted.

It’s done!

At this stage, you can log in, register or log out of your app using Parse Server core features through Back4App!