Android
Users
User registration - login
18 min
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 buildtoolsversion=30 0 2 , compile sdk version = 30 0 2 compile sdk version = 30 0 2 and targetsdkversion 30 targetsdkversion 30 at any time, you can access the complete android project built with this tutorial at our github repositories kotlin example repository java example repository goal we will learn how to log in and register using parse here is a preview of what we are gonna achive prerequisites to 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 tutoria l to create an android studio project connected to back4app a device (or virtual device ) running android 4 1 (jelly bean) or newer 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 2\ we will use lambda functions frequently in our project because of that we need to add java 1 8 to our project via build gradle(module\ app) 2 sign up signing up basically creates a new parse user object in user class, shown as “user” in your app dashboard dashboard we need to set at least two properties when creating a new user => parseuser setusername() parseuser setusername() and parseuser setpassword() parseuser setpassword() the method used for saving the new user on android is parseuser signupinbackground() parseuser signupinbackground() , which may come together with a callback function note objects of this special class are not saved on the dashboard dashboard with parseobject save() parseobject save() method to make signupactivity signupactivity work, follow these steps import into your signupactivity signupactivity , in addition to the dependencies imported in step 1 2\ to implement user registration, simply use the following code in the oncreate() oncreate() method 1 parseuser user = new parseuser(); 2 // set the user's username and password, which can be obtained by a forms 3 user setusername( "\<your username here>"); 4 user setpassword( "\<your password here>"); 5 user signupinbackground(new signupcallback() { 6 @override 7 public void done(parseexception e) { 8 if (e == null) { 9 showalert("successful sign up!", "welcome" + "\<your username here>" +"!"); 10 } else { 11 parseuser logout(); 12 toast maketext(signupactivity this, e getmessage(), toast length long) show(); 13 } 14 } 15 });1 val user = parseuser(); 2 // set the user's username and password, which can be obtained by a forms 3 user setusername("\<your username here>"); 4 user setpassword("\<your password here>"); 5 user signupinbackground(signupcallback() { 6 if (it == null) { 7 showalert("successful sign up!", "welcome" + "\<your username here>" + "!"); 8 } else { 9 parseuser logout(); 10 toast maketext(this, it message, toast length long) show(); 11 } 12 }); in the example project, this code is placed inside a sign up 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 private void showalert(string title,string message){ 2 alertdialog builder builder = new alertdialog builder(signupactivity this) 3 settitle(title) 4 setmessage(message) 5 setpositivebutton("ok", new dialoginterface onclicklistener() { 6 @override 7 public void onclick(dialoginterface dialog, int which) { 8 dialog cancel(); 9 // don't forget to change the line below with the names of your activities 10 intent intent = new intent(signupactivity this, logoutactivity class); 11 intent addflags(intent flag activity clear task | intent flag activity new task); 12 startactivity(intent); 13 } 14 }); 15 alertdialog ok = builder create(); 16 ok show(); 17 }1 private fun showalert(title string, message string) { 2 val builder = alertdialog builder(this) 3 settitle(title) 4 setmessage(message) 5 setpositivebutton("ok") { dialog, which > 6 dialog cancel() 7 // don't forget to change the line below with the names of your activities 8 val intent = intent(this, logoutactivity class java) 9 intent addflags(intent flag activity clear task or intent flag activity new task) 10 startactivity(intent) 11 } 12 val ok = builder create() 13 ok show() 14 } 3 log in logging in creates a session object, which points to the user logged in if login is successful, parseuser getcurrentuser() parseuser getcurrentuser() returns a user object, and a session object which created in the dashboard 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() 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 loginactivity work, follow these steps import into your loginactivity loginactivity , in addition to the dependencies imported in the step 1 2\ to implement user login function, simply use the code 1 private void login(string username, string password) { 2 progressdialog show(); 3 parseuser logininbackground(username, password, (parseuser, e) > { 4 progressdialog dismiss(); 5 if (parseuser != null) { 6 showalert("successful login", "welcome back " + username + " !"); 7 } else { 8 parseuser logout(); 9 toast maketext(loginactivity this, e getmessage(), toast length long) show(); 10 } 11 }); 12 }1 fun login(username string, password string) { 2 progressdialog? show() 3 parseuser logininbackground(username,password) { parseuser parseuser?, parseexception parseexception? > 4 progressdialog? dismiss() 5 if (parseuser != null) { 6 showalert("successful login", "welcome back " + username + " !") 7 } else { 8 parseuser logout() 9 if (parseexception != null) { 10 toast maketext(this, parseexception message, toast length long) show() 11 } 12 } 13 } 14 } in the example project, this code is placed inside a log in log in button callback also, username and password are caught using edit texts the method showalert showalert is the same that you added in the signupactivity signupactivity , don’t forget to change its intent intent arguments though 4 log out logging out deletes the active session object for the logged user the method used to perform log out is parseuser logoutinbackground() parseuser logoutinbackground() to implement user log out, simply use the code below, in the logoutactivity logoutactivity 1 parseuser logoutinbackground(e > { 2 progressdialog dismiss(); 3 if (e == null) 4 showalert("so, you're going ", "ok bye bye then"); 5 });1 fun login(username string, password string) { 2 progressdialog? show() 3 parseuser logininbackground(username,password) { parseuser parseuser?, parseexception parseexception? > 4 progressdialog? dismiss() 5 if (parseuser != null) { 6 showalert("successful login", "welcome back " + username + " !") 7 } else { 8 parseuser logout() 9 if (parseexception != null) { 10 toast maketext(this, parseexception message, toast length long) show() 11 } 12 } 13 } 14 } in the example project, this code is placed inside a log out log out button callback the method showalert showalert is the same that you added in the loginactivity loginactivity and signupactivity signupactivity , don’t forget to change its intent intent arguments though 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 dashboard > core core > browser browser > user 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 session is opened in your dashboard dashboard , but when the user logs out that particular session session ends also, whenever an unsuccessful login or sign up attempt occurs, the session session opened in parse server dashboard dashboard 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!