User Password Reset for React
Introduction
It’s a fact that as soon as you introduce passwords into a system, users will forget them. In such cases, the 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:
- A React App created and connected to Back4App.
- Complete the previous guides so you can have a better understanding of the Parse.User class.
- If you want to test/use the screen layout provided by this guide, you should set up the
Ant Design
library.
Goal
To add a user password reset feature to a React 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 Parse 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.
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 component. Remember to add a text input for your user email to your component.
1
2
3
4
5
6
7
8
9
10
11
12
13
const doRequestPasswordReset = async function () {
// Note that this value come from state variables linked to your text input
const emailValue = email;
try {
await Parse.User.requestPasswordReset(emailValue);
alert(`Success! Please check ${email} to proceed with password reset.`);
return true;
} catch(error) {
// Error can be caused by lack of Internet connection
alert(`Error! ${error}`);
return false;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
const doRequestPasswordReset = async function (): Promise<boolean> {
// Note that this value come from state variables linked to your text input
const emailValue: string = email;
try {
await Parse.User.requestPasswordReset(emailValue);
alert(`Success! Please check ${email} to proceed with password reset.`);
return true;
} catch(error: any) {
// Error can be caused by lack of Internet connection
alert(`Error! ${error}`);
return false;
}
};
Go ahead and test your component. After requesting a password reset email, you should have received the email, so check your inbox. Note that the message will contain any changes you had set up before in your Parse dashboard.
The password reset form will look like this:
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 guide’s 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
55
56
57
58
59
60
61
62
63
64
import React, { useState } from 'react';
import Parse from 'parse/dist/parse.min.js';
import './App.css';
import { Button, Divider, Input } from 'antd';
export const UserPasswordReset = () => {
// State variables
const [email, setEmail] = useState('');
// Functions used by the screen components
const doRequestPasswordReset = async function () {
// Note that this value come from state variables linked to your text input
const emailValue = email;
try {
await Parse.User.requestPasswordReset(emailValue);
alert(`Success! Please check ${email} to proceed with password reset.`);
return true;
} catch (error) {
// Error can be caused by lack of Internet connection
alert(`Error! ${error}`);
return false;
}
};
return (
<div>
<div className="header">
<img
className="header_logo"
alt="Back4App Logo"
src={
'https://blog.back4app.com/wp-content/uploads/2019/05/back4app-white-logo-500px.png'
}
/>
<p className="header_text_bold">{'React on Back4App'}</p>
<p className="header_text">{'User Password Reset'}</p>
</div>
<div className="container">
<h2 className="heading">{'Request password reset email'}</h2>
<Divider />
<div className="form_wrapper">
<Input
value={email}
onChange={(event) => setEmail(event.target.value)}
placeholder="Your account email"
size="large"
className="form_input"
/>
</div>
<div className="form_buttons">
<Button
onClick={() => doRequestPasswordReset()}
type="primary"
className="form_button"
color={'#208AEC'}
size="large"
>
Request password reset
</Button>
</div>
</div>
</div>
);
};
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
55
56
57
58
59
60
61
62
63
64
import React, { useState, FC, ReactElement } from 'react';
import './App.css';
import { Button, Divider, Input } from 'antd';
const Parse = require('parse/dist/parse.min.js');
export const UserPasswordReset: FC<{}> = (): ReactElement => {
// State variables
const [email, setEmail] = useState('');
// Functions used by the screen components
const doRequestPasswordReset = async function (): Promise<boolean> {
// Note that this value come from state variables linked to your text input
const emailValue: string = email;
try {
await Parse.User.requestPasswordReset(emailValue);
alert(`Success! Please check ${email} to proceed with password reset.`);
return true;
} catch(error: any) {
// Error can be caused by lack of Internet connection
alert(`Error! ${error}`);
return false;
}
};
return (
<div>
<div className="header">
<img
className="header_logo"
alt="Back4App Logo"
src={
'https://blog.back4app.com/wp-content/uploads/2019/05/back4app-white-logo-500px.png'
}
/>
<p className="header_text_bold">{'React on Back4App'}</p>
<p className="header_text">{'User Password Reset'}</p>
</div>
<div className="container">
<h2 className="heading">{'Request password reset email'}</h2>
<Divider />
<div className="form_wrapper">
<Input
value={email}
onChange={(event) => setEmail(event.target.value)}
placeholder="Your account email"
size="large"
className="form_input"
/>
</div>
<div className="form_buttons">
<Button
onClick={() => doRequestPasswordReset()}
type="primary"
className="form_button"
color={'#208AEC'}
size="large"
>
Request password reset
</Button>
</div>
</div>
</div>
);
};
Add these classes to your App.css
file to fully render the component layout elements:
App.css
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@import '~antd/dist/antd.css';
.App {
text-align: center;
}
html {
box-sizing: border-box;
outline: none;
overflow: auto;
}
*,
*:before,
*:after {
margin: 0;
padding: 0;
box-sizing: inherit;
}
h1,
h2,
h3,
h4,
h5,
h6 {
margin: 0;
font-weight: bold;
}
p {
margin: 0;
}
body {
margin: 0;
background-color: #fff;
}
.container {
width: 100%;
max-width: 500px;
margin: auto;
padding: 20px 0;
text-align: left;
}
.header {
align-items: center;
padding: 25px 0;
background-color: #208AEC;
}
.header_logo {
height: 55px;
margin-bottom: 20px;
object-fit: contain;
}
.header_text_bold {
margin-bottom: 3px;
color: rgba(255, 255, 255, 0.9);
font-size: 16px;
font-weight: bold;
}
.header_text {
color: rgba(255, 255, 255, 0.9);
font-size: 15px;
}
.heading {
font-size: 22px;
}
.form_wrapper {
margin-top: 20px;
margin-bottom: 10px;
}
.form_input {
margin-bottom: 20px;
}
.form_button {
width: 100%;
}
This component should render in a screen like this:
Conclusion
At the end of this guide, you learned how to allow your Parse users to reset their password on React. In the next guide, we will show you how to perform useful user queries.