Basic user registration

After this tutorial, you will be able to build a user system similar to this one.

 

MiXM7mL7aF

 

Make sure that you have passed through our Quickstart in order to connect your app with Back4App.

 

Quickstart

 

Step 1 – How to create login function

It is incredibly easy to create a login on Back4App. Here we will add it to a button click event (you can check how to here).
After instantiating the Button you will need to set the action when the user click on it:

login_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ParseUser.logInInBackground("Username", "Password", new LogInCallback() {
            @Override
            public void done(ParseUser parseUser, ParseException e) {
                if (parseUser != null) {
                   //Login Successful
                   //You may choose what to do or display here
                   //For example: Welcome + ParseUser.getUsername()
    
                } else {
                   //Login Fail
                   //get error by calling e.getMessage()
                }
            }
        });
    }
}

Step 2 – How to create register function

As simple as the login function.

You may place it inside a button setOnClickListener as well.

register_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ParseUser user = new ParseUser();
        user.setUsername(et_username.getText().toString());
        user.setPassword(et_password.getText().toString());
        user.signUpInBackground(new SignUpCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                   //Register Successful
                   //You may choose what to do or display here
                } else {
                   //Register Fail
                   //get error by calling e.getMessage()
                }
            }
        });
    }
}

Last, but not least, remember to save the new users to your database in Back4App (that can also be done within the same registering code).

Step 3 – Run your app

Test your app by registering any username and password.

Then go to Back4App’s Parse-Dashboard to check if the user data is successfully updated.

  1. https://dashboard.back4app.com/apps
  2. Login
  3. Parse Dashboard
  4. Check your database

Step 4 – Add your feature to your app

At this point the app isn’t perfect. You’ll need to add some loading and alert function to your app and make it realistic.

Check these examples for both loading and alert functions.

ProgressDialog – Loading

 

This will create a progress informing the user that the function (Login or Register) is being processed.

 

Inside onCreate (The same place where you have done the Parse.initialize()):

ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);

Inside button.setOnClickListener:

 

progressDialog.setMessage("Please Wait");
progressDialog.setTitle("Logging in");
progressDialog.show();
new Thread(new Runnable() {
   @Override
   public void run() {
      try {
         parseLogin();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}).start();

 

Parse Login Response

 

void parseLogin() {
   ParseUser.logInInBackground(et_username.getText().toString(), et_password.getText().toString(), new LogInCallback() {
      @Override
      public void done(ParseUser parseUser, ParseException e) {
         if (parseUser != null) {
            progressDialog.dismiss();
            alertDisplayer("Login Successful","Welcome "+parseUser.getUsername());
         } else {
            progressDialog.dismiss();
            alertDisplayer("Login Failed", e.getMessage()+" Please Try Again");
         }
      }
   });
}

 

Alert – Message to your user

 

This will create an alert that will inform your user of important informations, such as a successful login.

void alertDisplayer(String title,String message){
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
            .setTitle(title)
            .setMessage(message)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
    AlertDialog ok = builder.create();
    ok.show();
}

The result will be very similar to the example at the beginning of this tutorial.

Special Step – View a complete example at Github

In order to check a functional code for android login, please visit this Github repository:
Back4App Parse Android Login Example