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’ll learn how Parse.User class works by creating a user registration feature for your React Native App using Parse JS SDK.
To build a User Registration feature using Parse for a React Native 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 root 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(){2// Note that these values come from state variables that we've declared before3const usernameValue = username;4const passwordValue = password;5// Since the signUp method returns a Promise, we need to call it using await6returnawait Parse.User.signUp(usernameValue, passwordValue)7.then((createdUser)=>{8// Parse.User.signUp returns the already created ParseUser object if successful9 Alert.alert(10'Success!',11`User ${createdUser.getUsername()} was successfully created!`,12);13returntrue;14})15.catch((error)=>{16// signUp can fail if any parameter is blank or failed an uniqueness check on the server17 Alert.alert('Error!', error.message);18returnfalse;19});20};
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 onPress action to () => doUserRegistration() and to import Alert from react-native. Your component should now look like this:
UserRegistration.js
UserRegistration.tsx
1import React,{FC, ReactElement, useState }from"react";2import{ Alert, Button, StyleSheet, TextInput }from"react-native";3import Parse from"parse/react-native";45exportconst UserRegistration:FC<{}>=({}): ReactElement =>{6const[username, setUsername]=useState("");7const[password, setPassword]=useState("");89constdoUserRegistration=asyncfunction():Promise<boolean>{10// Note that these values come from state variables that we've declared before11const usernameValue:string= username;12const passwordValue:string= password;13// Since the signUp method returns a Promise, we need to call it using await14returnawait Parse.User.signUp(usernameValue, passwordValue)15.then((createdUser: Parse.User)=>{16// Parse.User.signUp returns the already created ParseUser object if successful17 Alert.alert(18"Success!",19`User ${createdUser.get("username")} was successfully created!`20);21returntrue;22})23.catch((error: object)=>{24// signUp can fail if any parameter is blank or failed an uniqueness check on the server25 Alert.alert("Error!", error.message);26returnfalse;27});28};2930return(31<>32<TextInput
33 style={styles.input}34 value={username}35 placeholder={"Username"}36 onChangeText={(text)=>setUsername(text)}37 autoCapitalize={"none"}38/>39<TextInput
40 style={styles.input}41 value={password}42 placeholder={"Password"}43 secureTextEntry
44 onChangeText={(text)=>setPassword(text)}45/>46<Button title={"Sign Up"} onPress={()=>doUserRegistration()}/>47</>48);49};5051const styles = StyleSheet.create({52 input:{53 height:40,54 marginBottom:10,55 backgroundColor:"#fff",56},57});
4 - Testing the component
The final step is to use our new component inside your React Native Application App.js file (or App.tsx if using TypeScript):
App.js
App.tsx
1import{ UserRegistration }from"./UserRegistration";2/*
3 Your functions here
4 */5return(6<>7<StatusBar />8<SafeAreaView style={styles.container}>9<>10<Text style={styles.title}>React Native on Back4App</Text>11<Text>User registration tutorial</Text>12<UserRegistration />13</>14</SafeAreaView>15</>16);1718// Remember to add some styles at the end of your file19const styles = StyleSheet.create({20container:{21flex:1,22backgroundColor:"#fff",23alignItems:"center",24justifyContent:"center",25},26title:{27fontSize:20,28fontWeight:"bold",29},30});
Your app now should look like this:
After providing the desired user credentials, you will see this message after pressing on Sign Up if everything was successful:
Error handling can be tested if you try to register a user with the same username as before:
You will get another error if you try to sign up with no password:
Conclusion
At the end of this guide, you learned how to register new Parse users on React Native. In the next guide, we will show you how to log in and out users.