Skip to content

Commit a644850

Browse files
committed
10-Password Reset and Password Change with Firebase
1 parent b329f6e commit a644850

File tree

4 files changed

+150
-13
lines changed

4 files changed

+150
-13
lines changed

src/components/Account/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import React from 'react';
22

3-
const Account = () => (
3+
import { PasswordForgetForm } from '../PasswordForget';
4+
import PasswordChangeForm from '../PasswordChange';
5+
6+
const AccountPage = () => (
47
<div>
5-
<h1>Account</h1>
8+
<h1>Account Page</h1>
9+
<PasswordForgetForm />
10+
<PasswordChangeForm />
611
</div>
712
);
813

9-
export default Account;
14+
export default AccountPage;
+67-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,69 @@
1-
import React from 'react';
1+
import React, { Component } from 'react';
22

3-
const PasswordChange = () => (
4-
<div>
5-
<h1>PasswordChange</h1>
6-
</div>
7-
);
3+
import { withFirebase } from '../Firebase';
84

9-
export default PasswordChange;
5+
const INITIAL_STATE = {
6+
passwordOne: '',
7+
passwordTwo: '',
8+
error: null,
9+
};
10+
11+
class PasswordChangeForm extends Component {
12+
constructor(props) {
13+
super(props);
14+
15+
this.state = { ...INITIAL_STATE };
16+
}
17+
18+
onSubmit = event => {
19+
const { passwordOne } = this.state;
20+
21+
this.props.firebase
22+
.doPasswordUpdate(passwordOne)
23+
.then(() => {
24+
this.setState({ ...INITIAL_STATE });
25+
})
26+
.catch(error => {
27+
this.setState({ error });
28+
});
29+
30+
event.preventDefault();
31+
};
32+
33+
onChange = event => {
34+
this.setState({ [event.target.name]: event.target.value });
35+
};
36+
37+
render() {
38+
const { passwordOne, passwordTwo, error } = this.state;
39+
40+
const isInvalid =
41+
passwordOne !== passwordTwo || passwordOne === '';
42+
43+
return (
44+
<form onSubmit={this.onSubmit}>
45+
<input
46+
name="passwordOne"
47+
value={passwordOne}
48+
onChange={this.onChange}
49+
type="password"
50+
placeholder="New Password"
51+
/>
52+
<input
53+
name="passwordTwo"
54+
value={passwordTwo}
55+
onChange={this.onChange}
56+
type="password"
57+
placeholder="Confirm New Password"
58+
/>
59+
<button disabled={isInvalid} type="submit">
60+
Reset My Password
61+
</button>
62+
63+
{error && <p>{error.message}</p>}
64+
</form>
65+
);
66+
}
67+
}
68+
69+
export default withFirebase(PasswordChangeForm);
+73-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,79 @@
1-
import React from 'react';
1+
import React, { Component } from 'react';
2+
import { Link } from 'react-router-dom';
23

3-
const PasswordForget = () => (
4+
import { withFirebase } from '../Firebase';
5+
import * as ROUTES from '../../constants/routes';
6+
7+
const PasswordForgetPage = () => (
48
<div>
59
<h1>PasswordForget</h1>
10+
<PasswordForgetForm />
611
</div>
712
);
813

9-
export default PasswordForget;
14+
const INITIAL_STATE = {
15+
email: '',
16+
error: null,
17+
};
18+
19+
class PasswordForgetFormBase extends Component {
20+
constructor(props) {
21+
super(props);
22+
23+
this.state = { ...INITIAL_STATE };
24+
}
25+
26+
onSubmit = event => {
27+
const { email } = this.state;
28+
29+
this.props.firebase
30+
.doPasswordReset(email)
31+
.then(() => {
32+
this.setState({ ...INITIAL_STATE });
33+
})
34+
.catch(error => {
35+
this.setState({ error });
36+
});
37+
38+
event.preventDefault();
39+
};
40+
41+
onChange = event => {
42+
this.setState({ [event.target.name]: event.target.value });
43+
};
44+
45+
render() {
46+
const { email, error } = this.state;
47+
48+
const isInvalid = email === '';
49+
50+
return (
51+
<form onSubmit={this.onSubmit}>
52+
<input
53+
name="email"
54+
value={this.state.email}
55+
onChange={this.onChange}
56+
type="text"
57+
placeholder="Email Address"
58+
/>
59+
<button disabled={isInvalid} type="submit">
60+
Reset My Password
61+
</button>
62+
63+
{error && <p>{error.message}</p>}
64+
</form>
65+
);
66+
}
67+
}
68+
69+
const PasswordForgetLink = () => (
70+
<p>
71+
<Link to={ROUTES.PASSWORD_FORGET}>Forgot Password?</Link>
72+
</p>
73+
);
74+
75+
export default PasswordForgetPage;
76+
77+
const PasswordForgetForm = withFirebase(PasswordForgetFormBase);
78+
79+
export { PasswordForgetForm, PasswordForgetLink };

src/components/SignIn/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { withRouter } from 'react-router-dom';
33
import { compose } from 'recompose';
44

55
import { SignUpLink } from '../SignUp';
6+
import { PasswordForgetLink } from '../PasswordForget';
67
import { withFirebase } from '../Firebase';
78
import * as ROUTES from '../../constants/routes';
89

910
const SignInPage = () => (
1011
<div>
1112
<h1>SignIn</h1>
1213
<SignInForm />
14+
<PasswordForgetLink />
1315
<SignUpLink />
1416
</div>
1517
);

0 commit comments

Comments
 (0)