React Native

User Password Reset for React Native

Introduction

It’s a fact that as soon as you introduce passwords into a system, users will forget them. In such cases, Parse library provides a way to let them securely reset their password.
As with email verification, Parse already has an implementation ready for this, Parse.User.requestPasswordEmail. By using this method, Parse will handle all aspects of password resetting for you seamlessly.

Prerequisites

To complete this tutorial, you will need:

Goal

To add a user password reset feature to a React Native App using Parse.

Step 1 - Customizing password reset emails

Before calling the Parse.User.requestPasswordEmail method, you can customize the message that your user will get after requesting a password reset. Log in to your App dashboard, go to Settings->Verification Emails and change your password reset email subject or message. Ensure that your user will receive an email containing clear instructions and indicating that it is indeed from your application.

React Native Back4App

Step 2 - Using requestPasswordEmail

Calling the Parse.User.requestPasswordEmail method only requires your user account email as a parameter, so go ahead and add the following function to your password reset screen. Remember to add a text input for your user email to your screen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const doUserPasswordReset = async function () {
  // Note that this value come from state variables linked to your text input
  const emailValue = email;
  return await Parse.User.requestPasswordReset(emailValue)
    .then(() => {
      // logIn returns the corresponding ParseUser object
      Alert.alert(
        'Success!',
        `Please check ${email} to proceed with password reset.`,
      );
      return true;
    })
    .catch((error) => {
      // Error can be caused by lack of Internet connection
      Alert.alert('Error!', error.message);
      return false;
    });
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const doUserPasswordReset = async function (): Promise<boolean> {
  // Note that this value come from state variables linked to your text input
  const emailValue: string = email;
  return await Parse.User.requestPasswordReset(emailValue)
    .then(() => {
      // logIn returns the corresponding ParseUser object
      Alert.alert(
        'Success!',
        `Please check ${email} to proceed with password reset.`,
      );
      return true;
    })
    .catch((error: object) => {
      // Error can be caused by lack of Internet connection
      Alert.alert('Error!', error.message);
      return false;
    });
};

Go ahead and test your screen and component. You will see a message like this after requesting a password reset email:

React Native Back4App

You should have received the email, so go ahead and check your inbox. Note that the message will contain any changes you had set up before in your Parse dashboard.

React Native Back4App

The password reset form will look like this:

React Native Back4App

That’s it, after changing the password in this form, your user will be able to log in again to your application.

Step 3 - Creating a password request component

As said before, you should create a component containing the function shown on Step 2 and also a text input field for your user account email to enable password reset in your app. Here is a complete example of this component. You can plug it in in our previous guides user login project if you like.

UserResetPassword.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import React, {FC, ReactElement, useState} from 'react';
import {Alert, Text, TextInput, TouchableOpacity, View} from 'react-native';
import Parse from 'parse/react-native';
import {useNavigation} from '@react-navigation/native';
import Styles from './Styles';

export const UserResetPassword = () => {
  const navigation = useNavigation();

  // Your state variable
  const [email, setEmail] = useState('');

  const doUserPasswordReset = async function (): Promise<boolean> {
    // Note that this value come from state variables linked to your text input
    const emailValue = email;
    return await Parse.User.requestPasswordReset(emailValue)
      .then(() => {
        // logIn returns the corresponding ParseUser object
        Alert.alert(
          'Success!',
          `Please check ${email} to proceed with password reset.`,
        );
        // Redirect user to your login screen
        navigation.navigate('Login');
        return true;
      })
      .catch((error) => {
        // Error can be caused by lack of Internet connection
        Alert.alert('Error!', error.message);
        return false;
      });
  };

  return (
    <View style={Styles.login_wrapper}>
      <View style={Styles.form}>
        <Text>{'Please enter your account email to reset your password:'}</Text>
        <TextInput
          style={Styles.form_input}
          value={email}
          placeholder={'Your account email'}
          onChangeText={(text) => setEmail(text)}
          autoCapitalize={'none'}
          keyboardType={'email-address'}
        />
        <TouchableOpacity onPress={() => doUserPasswordReset()}>
          <View style={Styles.button}>
            <Text style={Styles.button_label}>{'Request password reset'}</Text>
          </View>
        </TouchableOpacity>
      </View>
    </View>
  );
};

UserResetPassword.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import React, {FC, ReactElement, useState} from 'react';
import {Alert, Text, TextInput, TouchableOpacity, View} from 'react-native';
import Parse from 'parse/react-native';
import {useNavigation} from '@react-navigation/native';
import Styles from './Styles';

export const UserResetPassword: FC<{}> = ({}): ReactElement => {
  const navigation = useNavigation();

  // Your state variable
  const [email, setEmail] = useState('');

  const doUserPasswordReset = async function (): Promise<boolean> {
    // Note that this value come from state variables linked to your text input
    const emailValue: string = email;
    return await Parse.User.requestPasswordReset(emailValue)
      .then(() => {
        // logIn returns the corresponding ParseUser object
        Alert.alert(
          'Success!',
          `Please check ${email} to proceed with password reset.`,
        );
        // Redirect user to your login screen
        navigation.navigate('Login');
        return true;
      })
      .catch((error: object) => {
        // Error can be caused by lack of Internet connection
        Alert.alert('Error!', error.message);
        return false;
      });
  };

  return (
    <View style={Styles.login_wrapper}>
      <View style={Styles.form}>
        <Text>{'Please enter your account email to reset your password:'}</Text>
        <TextInput
          style={Styles.form_input}
          value={email}
          placeholder={'Your account email'}
          onChangeText={(text) => setEmail(text)}
          autoCapitalize={'none'}
          keyboardType={'email-address'}
        />
        <TouchableOpacity onPress={() => doUserPasswordReset()}>
          <View style={Styles.button}>
            <Text style={Styles.button_label}>{'Request password reset'}</Text>
          </View>
        </TouchableOpacity>
      </View>
    </View>
  );
};

This component should render in a screen like this:

React Native Back4App

Conclusion

At the end of this guide, you learned how to allow your Parse users to reset their password on React Native. In the next guide, we will show you how to perform useful user queries.