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 https //www back4app com/product/parse server through back4app this tutorial uses a basic app created in android studio 4 1 1 with \<font color="#2166ae">buildtoolsversion=30 0 2\</font> , \<font color="#2166ae">compile sdk version = 30 0 2\</font> and \<font color="#2166ae">targetsdkversion 30\</font> at any time, you can access the complete android project built with this tutorial at our github repositories kotlin example repository https //github com/templates back4app/android parse sdk kotlin java example repository https //github com/templates back4app/android parse sdk java 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 https //developer android com/studio/index html an app created on back4app note follow the new parse app tutorial https //www back4app com/docs/get started/new parse app to learn how to create a parse app on back4app an android app connected to back4app note follow the install parse sdk tutoria https //www back4app com/docs/android/parse android sdk l to create an android studio project connected to back4app a device (or virtual device https //developer android com/studio/run/managing avds html ) 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 1 import com parse parse ; 2 import com parse parseexception ; 3 import com parse parseuser ; 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) 1 compileoptions { 2 sourcecompatibility javaversion version 1 8 3 targetcompatibility javaversion version 1 8 4 } 2 sign up signing up basically creates a new parse user object in user class, shown as “user” in your app \<font color="#2166ae">dashboard\</font> we need to set at least two properties when creating a new user => \<font color="#2166ae">parseuser setusername()\</font> and \<font color="#2166ae">parseuser setpassword()\</font> the method used for saving the new user on android is \<font color="#2166ae">parseuser signupinbackground()\</font> , which may come together with a callback function note objects of this special class are not saved on the \<font color="#2166ae">dashboard\</font> with \<font color="#2166ae">parseobject save()\</font> method to make \<font color="#2166ae">signupactivity\</font> work, follow these steps import 1 import com parse signupcallback ; into your \<font color="#2166ae">signupactivity\</font> , in addition to the dependencies imported in step 1 2\ to implement user registration, simply use the following code in the \<font color="#2166ae">oncreate()\</font> 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 \<font color="#2166ae">sign up\</font> 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, \<font color="#2166ae">parseuser getcurrentuser()\</font> returns a user object, and a session object which created in the \<font color="#2166ae">dashboard\</font> 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 \<font color="#2166ae">parseuser logininbackground()\</font> , 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 \<font color="#2166ae">loginactivity\</font> work, follow these steps import into your \<font color="#2166ae">loginactivity\</font> , 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 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 \<font color="#2166ae">log in\</font> button callback also, username and password are caught using edit texts the method \<font color="#2166ae">showalert\</font> is the same that you added in the \<font color="#2166ae">signupactivity\</font> , don’t forget to change its \<font color="#2166ae">intent\</font> arguments though 4 log out logging out deletes the active session object for the logged user the method used to perform log out is \<font color="#2166ae">parseuser logoutinbackground()\</font> to implement user log out, simply use the code below, in the \<font color="#2166ae">logoutactivity\</font> 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 \<font color="#2166ae">log out\</font> button callback the method \<font color="#2166ae">showalert\</font> is the same that you added in the \<font color="#2166ae">loginactivity\</font> and \<font color="#2166ae">signupactivity\</font> , don’t forget to change its \<font color="#2166ae">intent\</font> 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 https //www back4app com/ find your app and click on \<font color="#2166ae">dashboard\</font> > \<font color="#2166ae">core\</font> > \<font color="#2166ae">browser\</font> > \<font color="#2166ae">user\</font> 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 \<font color="#2166ae">session\</font> is opened in your \<font color="#2166ae">dashboard\</font> , but when the user logs out that particular \<font color="#2166ae">session\</font> ends also, whenever an unsuccessful login or sign up attempt occurs, the \<font color="#2166ae">session\</font> opened in parse server \<font color="#2166ae">dashboard\</font> 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!