Skip to content

Commit

Permalink
10-Password Reset and Password Change with Firebase
Browse files Browse the repository at this point in the history
  • Loading branch information
rwieruch committed Nov 20, 2018
1 parent b329f6e commit a644850
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 13 deletions.
11 changes: 8 additions & 3 deletions src/components/Account/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import React from 'react';

const Account = () => (
import { PasswordForgetForm } from '../PasswordForget';
import PasswordChangeForm from '../PasswordChange';

const AccountPage = () => (
<div>
<h1>Account</h1>
<h1>Account Page</h1>
<PasswordForgetForm />
<PasswordChangeForm />
</div>
);

export default Account;
export default AccountPage;
74 changes: 67 additions & 7 deletions src/components/PasswordChange/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,69 @@
import React from 'react';
import React, { Component } from 'react';

const PasswordChange = () => (
<div>
<h1>PasswordChange</h1>
</div>
);
import { withFirebase } from '../Firebase';

export default PasswordChange;
const INITIAL_STATE = {
passwordOne: '',
passwordTwo: '',
error: null,
};

class PasswordChangeForm extends Component {
constructor(props) {
super(props);

this.state = { ...INITIAL_STATE };
}

onSubmit = event => {
const { passwordOne } = this.state;

this.props.firebase
.doPasswordUpdate(passwordOne)
.then(() => {
this.setState({ ...INITIAL_STATE });
})
.catch(error => {
this.setState({ error });
});

event.preventDefault();
};

onChange = event => {
this.setState({ [event.target.name]: event.target.value });
};

render() {
const { passwordOne, passwordTwo, error } = this.state;

const isInvalid =
passwordOne !== passwordTwo || passwordOne === '';

return (
<form onSubmit={this.onSubmit}>
<input
name="passwordOne"
value={passwordOne}
onChange={this.onChange}
type="password"
placeholder="New Password"
/>
<input
name="passwordTwo"
value={passwordTwo}
onChange={this.onChange}
type="password"
placeholder="Confirm New Password"
/>
<button disabled={isInvalid} type="submit">
Reset My Password
</button>

{error && <p>{error.message}</p>}
</form>
);
}
}

export default withFirebase(PasswordChangeForm);
76 changes: 73 additions & 3 deletions src/components/PasswordForget/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,79 @@
import React from 'react';
import React, { Component } from 'react';
import { Link } from 'react-router-dom';

const PasswordForget = () => (
import { withFirebase } from '../Firebase';
import * as ROUTES from '../../constants/routes';

const PasswordForgetPage = () => (
<div>
<h1>PasswordForget</h1>
<PasswordForgetForm />
</div>
);

export default PasswordForget;
const INITIAL_STATE = {
email: '',
error: null,
};

class PasswordForgetFormBase extends Component {
constructor(props) {
super(props);

this.state = { ...INITIAL_STATE };
}

onSubmit = event => {
const { email } = this.state;

this.props.firebase
.doPasswordReset(email)
.then(() => {
this.setState({ ...INITIAL_STATE });
})
.catch(error => {
this.setState({ error });
});

event.preventDefault();
};

onChange = event => {
this.setState({ [event.target.name]: event.target.value });
};

render() {
const { email, error } = this.state;

const isInvalid = email === '';

return (
<form onSubmit={this.onSubmit}>
<input
name="email"
value={this.state.email}
onChange={this.onChange}
type="text"
placeholder="Email Address"
/>
<button disabled={isInvalid} type="submit">
Reset My Password
</button>

{error && <p>{error.message}</p>}
</form>
);
}
}

const PasswordForgetLink = () => (
<p>
<Link to={ROUTES.PASSWORD_FORGET}>Forgot Password?</Link>
</p>
);

export default PasswordForgetPage;

const PasswordForgetForm = withFirebase(PasswordForgetFormBase);

export { PasswordForgetForm, PasswordForgetLink };
2 changes: 2 additions & 0 deletions src/components/SignIn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { withRouter } from 'react-router-dom';
import { compose } from 'recompose';

import { SignUpLink } from '../SignUp';
import { PasswordForgetLink } from '../PasswordForget';
import { withFirebase } from '../Firebase';
import * as ROUTES from '../../constants/routes';

const SignInPage = () => (
<div>
<h1>SignIn</h1>
<SignInForm />
<PasswordForgetLink />
<SignUpLink />
</div>
);
Expand Down

0 comments on commit a644850

Please sign in to comment.