Step 1 - Set up
At the beginning of each Parse activity, import the following:1 2 3 4 5 6 7 8 9 10 11 | 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; import com.parse.Parse; import com.parse.ParseException; import com.parse.ParseUser; |
Step 2 - Sign Up
Signing up basically involves saving a new object of class ParseUser, shown as “User” in your appDashboard
, 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:- Import into your
SignUpActivity
, in addition to the dependencies imported in Step 1:1
import com.parse.SignUpCallback;
- 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. - 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 theDashboard
. 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
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 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.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 methodalertDisplayer
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
.
Note: Using the codes displayed above, every time you log in with a user, aSession
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!
At this stage, you can log in, register or log out of your app using Parse Server core features through Back4App!Login RegistrationSource
back4app CRUD operation
Adminca