Skip to content

Commit 6cbccce

Browse files
committed
06-Sign In with React and Firebase
1 parent d5d2ba5 commit 6cbccce

File tree

1 file changed

+82
-3
lines changed

1 file changed

+82
-3
lines changed

src/components/SignIn/index.js

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,88 @@
1-
import React from 'react';
1+
import React, { Component } from 'react';
2+
import { withRouter } from 'react-router-dom';
3+
import { compose } from 'recompose';
24

3-
const SignIn = () => (
5+
import { SignUpLink } from '../SignUp';
6+
import { withFirebase } from '../Firebase';
7+
import * as ROUTES from '../../constants/routes';
8+
9+
const SignInPage = () => (
410
<div>
511
<h1>SignIn</h1>
12+
<SignInForm />
13+
<SignUpLink />
614
</div>
715
);
816

9-
export default SignIn;
17+
const INITIAL_STATE = {
18+
email: '',
19+
password: '',
20+
error: null,
21+
};
22+
23+
class SignInFormBase extends Component {
24+
constructor(props) {
25+
super(props);
26+
27+
this.state = { ...INITIAL_STATE };
28+
}
29+
30+
onSubmit = event => {
31+
const { email, password } = this.state;
32+
33+
this.props.firebase
34+
.doSignInWithEmailAndPassword(email, password)
35+
.then(() => {
36+
this.setState({ ...INITIAL_STATE });
37+
this.props.history.push(ROUTES.HOME);
38+
})
39+
.catch(error => {
40+
this.setState({ error });
41+
});
42+
43+
event.preventDefault();
44+
};
45+
46+
onChange = event => {
47+
this.setState({ [event.target.name]: event.target.value });
48+
};
49+
50+
render() {
51+
const { email, password, error } = this.state;
52+
53+
const isInvalid = password === '' || email === '';
54+
55+
return (
56+
<form onSubmit={this.onSubmit}>
57+
<input
58+
name="email"
59+
value={email}
60+
onChange={this.onChange}
61+
type="text"
62+
placeholder="Email Address"
63+
/>
64+
<input
65+
name="password"
66+
value={password}
67+
onChange={this.onChange}
68+
type="password"
69+
placeholder="Password"
70+
/>
71+
<button disabled={isInvalid} type="submit">
72+
Sign In
73+
</button>
74+
75+
{error && <p>{error.message}</p>}
76+
</form>
77+
);
78+
}
79+
}
80+
81+
const SignInForm = compose(
82+
withRouter,
83+
withFirebase,
84+
)(SignInFormBase);
85+
86+
export default SignInPage;
87+
88+
export { SignInForm };

0 commit comments

Comments
 (0)