Login and User registration tutorial
Introduction
In this section we will take a look to how to create an app with a simple user registration using Parse Server core features through Back4App.
This tutorial uses a basic app created in Android Studio 4.1.1 with buildToolsVersion=30.0.2
, Compile SDK Version = 30.0.2
and targetSdkVersion 30
At any time, you can access the complete Project via our GitHub repositories.
Goal
We will learn how to log in and register using Parse.
Here is a preview of what we are gonna achive :
Prerequisites
For complete this tutorial, we need:
- Android Studio.
- An app created on Back4App.
- Note: Follow the New Parse App tutorial to learn how to create a Parse App on Back4App.
- An android app connected to Back4app.
- Note: Follow the Install Parse SDK tutorial to create an Android Studio Project connected to Back4App.
- A device (or virtual device) running Android 4.1 (Jelly Bean) or newer.
Step 1 - Import Library
In this step we will import the libraries which we are gonna use in our project:
-
We will add following Parse Classes to our Activities.
1 2 3
import com.parse.Parse; import com.parse.ParseException; import com.parse.ParseUser;
-
We will use lambda functions frequently in our project because of that we need to add
Java 1.8
to our project viabuild.gradle(Module:App)
1 2 3 4
compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }
Step 2 - Sign Up
Signing up basically creates a new Parse.User Object in User Class, shown as “User” in your app Dashboard
. We need to set at least two properties when creating a new user => ParseUser.setUsername()
and ParseUser.setPassword()
.
The method used for saving the new user on Android is ParseUser.signUpInBackground()
, which may come together with a callback function.
Note: Objects of this special class are not saved on the
Dashboard
withParseObject.save()
method.
To make SignUpActivity
work, follow these steps:
- Import
1
import com.parse.SignUpCallback;
into your
SignUpActivity
, in addition to the dependencies imported in Step 1. -
To implement user registration, simply use the following code in the
onCreate()
method: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( "<Your username here>"); user.setPassword( "<Your password here>"); user.signUpInBackground(new SignUpCallback() { @Override public void done(ParseException e) { if (e == null) { showAlert("Successful Sign Up!", "Welcome" + "<Your username here>" +"!"); } else { ParseUser.logOut(); Toast.makeText(SignUpActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); } } });
1 2 3 4 5 6 7 8 9 10 11 12
val user = ParseUser(); // Set the user's username and password, which can be obtained by a forms user.setUsername("<Your username here>"); user.setPassword("<Your password here>"); user.signUpInBackground(SignUpCallback() { if (it == null) { showAlert("Successful Sign Up!", "Welcome" + "<Your username here>" + "!"); } else { ParseUser.logOut(); Toast.makeText(this, it.message, 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. - 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 showAlert(String title,String message){ AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.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(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
private fun showAlert(title: String, message: String) { val builder = AlertDialog.Builder(this) .setTitle(title) .setMessage(message) .setPositiveButton("OK") { dialog, which -> dialog.cancel() // don't forget to change the line below with the names of your Activities val intent = Intent(this, LogoutActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK) startActivity(intent) } val 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 which 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:
- Import into your
LoginActivity
, in addition to the dependencies imported in the Step 1:1
import com.parse.LogInCallback;
-
To implement user login function, simply use the code:
1 2 3 4 5 6 7 8 9 10 11 12
private void login(String username, String password) { progressDialog.show(); ParseUser.logInInBackground(username, password, (parseUser, e) -> { progressDialog.dismiss(); if (parseUser != null) { showAlert("Successful Login", "Welcome back " + username + " !"); } else { ParseUser.logOut(); Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); } }); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
fun login(username: String, password: String) { progressDialog?.show() ParseUser.logInInBackground(username,password) { parseUser: ParseUser?, parseException: ParseException? -> progressDialog?.dismiss() if (parseUser != null) { showAlert("Successful Login", "Welcome back " + username + " !") } else { ParseUser.logOut() if (parseException != null) { Toast.makeText(this, parseException.message, 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
showAlert
is the same that you added in theSignUpActivity
, don’t forget to change itsIntent
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.logOutInBackground()
.
To implement user log out, simply use the code below, in the LogoutActivity
:
1
2
3
4
5
ParseUser.logOutInBackground(e -> {
progressDialog.dismiss();
if (e == null)
showAlert("So, you're going...", "Ok...Bye-bye then");
});
1
2
3
4
5
ParseUser.logOutInBackground { e: ParseException? ->
progressDialog!!.dismiss()
if (e == null)
showAlert("So, you're going...", "Ok...Bye-bye then")
}
In the example project, this code is placed inside a
LOG OUT
button callback.
The method
showAlert
is the same that you added in theLoginActivity
andSignUpActivity
, don’t forget to change itsIntent
arguments though.
Step 5 - Test your app
- Run your app and create a couple of users, also try logging in again after registering them.
- Login at Back4App Website.
- Find your app and click on
Dashboard
>Core
>Browser
>User
.
At this point, you should see your users as displayed below:

Note: Using the codes displayed above, every time you log in with a user, a
Session
is opened in yourDashboard
, but when the user logs out that particularSession
ends. Also, whenever an unsuccessful login or sign up attempt occurs, theSession
opened in Parse ServerDashboard
is deleted.
It’s done!
Congrats ! Now you can log in, register or log out of your app using Parse Server core features through Back4App!