JavaScript
User Registration - login
12 min
add javascript user registration and log in to your parse app introduction this section explains how to do a basic user registration with email verification in a javascript environment through back4app in this tutorial, you will use parse user parse user object and will learn its most important functions see more about parse sdk at parse javascript sdk api reference and parse open source documentation for javascript sdk prerequisites to complete this tutorial, you will need a basic javascript app connected with back4app or jsbin connected with our parse api note you can use the app created in our javascript install parse sdk tutorial or use the same online environment jsbin with the setup done in the j avascript database operations tutorial 1 sign up the user sign up function is similar to the create create function used in the javascript database operations tutorial , but it has some additional benefits check if the username and email are unique securely hashes the password in the cloud not even the developer can see the user’s password requires at least a username and a password you can use the email as a username if you want to you can open the back4app javascript sign up function to see the code that has already been implemented to make your own signup function, you need to repeat the same steps of the create function explained in the javascript crud tutorial but call the method user signup instead of the save method, as shown below signup js signup(); function signup() { // create a new instance of the user class var user = new parse user(); user set("username", "my name"); user set("password", "my pass"); user set("email", "email\@example com"); // other fields can be set just like with parse object user set("phone", "415 392 0202"); user signup() then(function(user) { console log('user created successful with name ' + user get("username") + ' and email ' + user get("email")); }) catch(function(error){ console log("error " + error code + " " + error message); }); } be aware that error 202 or error 203 is likely to occur if you don’t change the username or the email the error 209 invalid season token is also likely to occur when your browser cookies conflict with your parse’s current session to bypass that, clear your browser cookies or open the incognito mode of your browser to confirm that the new user has been added to the database, you can access your parse dashboard parse dashboard or code the login login function which will be provided ahead 2 email verification an important feature of a sign up method is email verification fortunately, it is easy to configure it using back4app to enable email verification, login to your account, find your app, and click on server settings server settings find the “verification emails” box and click on settings settings here’s how the “verification emails” box looks like this then enable the verification by checking the box below if you are using the cloud environment of jsbin, then there is no need to complete this step by enabling this, the user’s class in your database receives one additional field verifiedemail verifiedemail this field is set to true when the email is verified, false if the email isn’t verified, and undefined if the user was created before this setting was checked on that page, you can also customize the email, change the subject, the body, and the sender’s email and name to see how the email looks like, just create a user, using the signup signup function, with an email that you can access you should receive an email for verification 3 login the login function is very simple and just needs a password and a username to run you can open the back4app javascript login function to see the code that has already been implemented you need to call the parse user login parse user login method is as follows login js login(); function login() { // create a new instance of the user class var user = parse user login("myname", "mypass") then(function(user) { console log('user created successful with name ' + user get("username") + ' and email ' + user get("email")); }) catch(function(error){ console log("error " + error code + " " + error message); }); } 4 reset password it’s very important to add the reset password option as users are likely to forget their password in the future the configuration for the email that will be sent in the reset password function is on the same page as in the email verification step there you can change the body and the subject of the email you can open the back4app javascript reset password function to see the code that has already been implemented to send the reset password email, just run the following code resetpassword js resetpassword(); function resetpassword() { parse user requestpasswordreset("email\@example com") then(function() { console log("password reset request was sent successfully"); }) catch(function(error) { console log("the login failed with error " + error code + " " + error message); }); } it’s done! at this point, you have learned not only how to do user registration with javascript apps, but also how to send email verification and password reset emails