At the core of many apps, user accounts have a notion that lets users securely access their information. Parse provides a specialized user class called Parse.User that automatically handles much of the functionality required for user account management.
In this guide, you will learn how the Parse.User class works by creating a user registration feature for your React App using Parse JS SDK.
If you want to test/use the screen layout provided by this guide, you should set up the Ant Design library.
Goal
To build a User Registration feature using Parse for a React App.
1 - Understanding the SignUp method
Parse User management uses the Parse.User object type, which extends the default ParseObject type while containing unique helper methods, such as current and getUsername, that will help you retrieve user data throughout your app. You can read more about the Parse.User object here at the official documentation.
In this guide, you will learn how to use the signUp method that creates a new valid and unique Parse.User object both locally and on the server, taking as arguments valid username and password values.
2 - Create the user registration component
Let’s now build the functional component, which will call the signUp method in our App. First, create a new file in your src directory called UserRegistration.js (UserRegistration.tsx if you are using TypeScript) and also add the needed input elements (username and password inputs), using state hooks via useState to manage their data:
You can now create the sign-up function that will call the signUp method:
JavaScript
TypeScript
1constdoUserRegistration=asyncfunction():Promise<boolean>{2// Note that these values come from state variables that we've declared before3const usernameValue:string= username;4const passwordValue:string= password;5try{6// Since the signUp method returns a Promise, we need to call it using await7const createdUser: Parse.User =await Parse.User.signUp(usernameValue, passwordValue);8alert(9`Success! User ${createdUser.getUsername()} was successfully created!`,10);11returntrue;12}catch(error:any){13// signUp can fail if any parameter is blank or failed an uniqueness check on the server14alert(`Error! ${error}`);15returnfalse;16};17};
Note: Creating a new user using signUp also makes it the currently logged-in user, so there is no need for your user to log in again to continue using your App.
Insert this function inside the UserRegistration component, just before the return call, to be called and tested. Remember to update the form’s sign up button onClick action to () => doUserRegistration(). Your component should now look like this:
UserRegistration.js
UserRegistration.tsx
1import React,{ useState,FC, ReactElement }from'react';2import'./App.css';3import{ Button, Divider, Input }from'antd';4const Parse =require('parse/dist/parse.min.js');56exportconst UserRegistration:FC<{}>=(): ReactElement =>{7// State variables8const[username, setUsername]=useState('');9const[password, setPassword]=useState('');1011// Functions used by the screen components12constdoUserRegistration=asyncfunction():Promise<boolean>{13// Note that these values come from state variables that we've declared before14const usernameValue:string= username;15const passwordValue:string= password;16try{17// Since the signUp method returns a Promise, we need to call it using await18const createdUser: Parse.User =await Parse.User.signUp(usernameValue, passwordValue);19alert(20`Success! User ${createdUser.getUsername()} was successfully created!`,21);22returntrue;23}catch(error:any){24// signUp can fail if any parameter is blank or failed an uniqueness check on the server25alert(`Error! ${error}`);26returnfalse;27};28};2930return(31<div>32<div className="header">33<img
34 className="header_logo"35 alt="Back4App Logo"36 src={37'https://blog.back4app.com/wp-content/uploads/2019/05/back4app-white-logo-500px.png'38}39/>40<p className="header_text_bold">{'React on Back4App'}</p>41<p className="header_text">{'User Registration'}</p>42</div>43<div className='container'>44<h2 className="heading">{'User Registration'}</h2>45<Divider />46<div className="form_wrapper">47<Input
48 value={username}49 onChange={(event)=>setUsername(event.target.value)}50 placeholder="Username"51 size="large"52 className="form_input"53/>54<Input
55 value={password}56 onChange={(event)=>setPassword(event.target.value)}57 placeholder="Password"58 size="large"59 type="password"60 className="form_input"61/>62</div>63<div className="form_buttons">64<Button
65 onClick={()=>doUserRegistration()}66 type="primary"67 className="form_button"68 color={'#208AEC'}69 size="large"70>71 Sign Up
72</Button>73</div>74</div>75</div>76);77};
Also add these classes to your App.css file to fully render the layout styles: