Skip to content

Commit d5d2ba5

Browse files
committed
05-Sign Up with React and Firebase
1 parent ad71eae commit d5d2ba5

File tree

5 files changed

+180
-6
lines changed

5 files changed

+180
-6
lines changed

package-lock.json

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"react": "^16.6.3",
88
"react-dom": "^16.6.3",
99
"react-router-dom": "^4.3.1",
10-
"react-scripts": "2.1.1"
10+
"react-scripts": "2.1.1",
11+
"recompose": "^0.30.0"
1112
},
1213
"scripts": {
1314
"start": "react-scripts start",

src/components/Firebase/context.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@ import React from 'react';
22

33
const FirebaseContext = React.createContext(null);
44

5+
export const withFirebase = Component => props => (
6+
<FirebaseContext.Consumer>
7+
{firebase => <Component {...props} firebase={firebase} />}
8+
</FirebaseContext.Consumer>
9+
);
10+
511
export default FirebaseContext;

src/components/Firebase/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import FirebaseContext from './context';
1+
import FirebaseContext, { withFirebase } from './context';
22
import Firebase from './firebase';
33

44
export default Firebase;
55

6-
export { FirebaseContext };
6+
export { FirebaseContext, withFirebase };

src/components/SignUp/index.js

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,114 @@
1-
import React from 'react';
1+
import React, { Component } from 'react';
2+
import { Link, withRouter } from 'react-router-dom';
23

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

9-
export default SignUp;
14+
const INITIAL_STATE = {
15+
username: '',
16+
email: '',
17+
passwordOne: '',
18+
passwordTwo: '',
19+
error: null,
20+
};
21+
22+
class SignUpFormBase extends Component {
23+
constructor(props) {
24+
super(props);
25+
26+
this.state = { ...INITIAL_STATE };
27+
}
28+
29+
onSubmit = event => {
30+
const { username, email, passwordOne } = this.state;
31+
32+
this.props.firebase
33+
.doCreateUserWithEmailAndPassword(email, passwordOne)
34+
.then(authUser => {
35+
this.setState({ ...INITIAL_STATE });
36+
this.props.history.push(ROUTES.HOME);
37+
})
38+
.catch(error => {
39+
this.setState({ error });
40+
});
41+
42+
event.preventDefault();
43+
};
44+
45+
onChange = event => {
46+
this.setState({ [event.target.name]: event.target.value });
47+
};
48+
49+
render() {
50+
const {
51+
username,
52+
email,
53+
passwordOne,
54+
passwordTwo,
55+
error,
56+
} = this.state;
57+
58+
const isInvalid =
59+
passwordOne !== passwordTwo ||
60+
passwordOne === '' ||
61+
email === '' ||
62+
username === '';
63+
64+
return (
65+
<form onSubmit={this.onSubmit}>
66+
<input
67+
name="username"
68+
value={username}
69+
onChange={this.onChange}
70+
type="text"
71+
placeholder="Full Name"
72+
/>
73+
<input
74+
name="email"
75+
value={email}
76+
onChange={this.onChange}
77+
type="text"
78+
placeholder="Email Address"
79+
/>
80+
<input
81+
name="passwordOne"
82+
value={passwordOne}
83+
onChange={this.onChange}
84+
type="password"
85+
placeholder="Password"
86+
/>
87+
<input
88+
name="passwordTwo"
89+
value={passwordTwo}
90+
onChange={this.onChange}
91+
type="password"
92+
placeholder="Confirm Password"
93+
/>
94+
<button disabled={isInvalid} type="submit">
95+
Sign Up
96+
</button>
97+
98+
{error && <p>{error.message}</p>}
99+
</form>
100+
);
101+
}
102+
}
103+
104+
const SignUpLink = () => (
105+
<p>
106+
Don't have an account? <Link to={ROUTES.SIGN_UP}>Sign Up</Link>
107+
</p>
108+
);
109+
110+
const SignUpForm = withRouter(withFirebase(SignUpFormBase));
111+
112+
export default SignUpPage;
113+
114+
export { SignUpForm, SignUpLink };

0 commit comments

Comments
 (0)