|
| 1 | +import React from 'react'; |
| 2 | +import { firebaseConnect } from 'react-redux-firebase'; |
| 3 | +import { connect } from 'react-redux'; |
| 4 | +import { compose } from 'redux'; |
| 5 | +import { Redirect, Link } from 'react-router-dom'; |
| 6 | + |
| 7 | +class PageLogin extends React.Component { |
| 8 | + constructor(props) { |
| 9 | + super(props); |
| 10 | + this.state = { |
| 11 | + email: '', |
| 12 | + password: '', |
| 13 | + }; |
| 14 | + } |
| 15 | + |
| 16 | + handleChange = event => |
| 17 | + this.setState({ [event.target.name]: event.target.value, error: '' }); |
| 18 | + |
| 19 | + login = async () => { |
| 20 | + const credentials = { |
| 21 | + email: this.state.email, |
| 22 | + password: this.state.password, |
| 23 | + }; |
| 24 | + |
| 25 | + try { |
| 26 | + await this.props.firebase.login(credentials); |
| 27 | + } catch (error) { |
| 28 | + this.setState({ error: error.message }); |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + render() { |
| 33 | + if (this.props.isLoggedIn) { |
| 34 | + return <Redirect to="/" />; |
| 35 | + } |
| 36 | + |
| 37 | + return ( |
| 38 | + <div> |
| 39 | + <h2>Login</h2> |
| 40 | + <div> |
| 41 | + <div>{this.state.error}</div> |
| 42 | + <input |
| 43 | + name="email" |
| 44 | + onChange={this.handleChange} |
| 45 | + placeholder="Email" |
| 46 | + value={this.state.email} |
| 47 | + /> |
| 48 | + <br /> |
| 49 | + <input |
| 50 | + name="password" |
| 51 | + onChange={this.handleChange} |
| 52 | + placeholder="Password" |
| 53 | + type="password" |
| 54 | + value={this.state.password} |
| 55 | + /> |
| 56 | + </div> |
| 57 | + <br /> |
| 58 | + <button onClick={this.login}>Login!</button> |
| 59 | + <hr /> |
| 60 | + <Link to="/">Home</Link> |
| 61 | + <br /> |
| 62 | + <Link to="/register">Register</Link> |
| 63 | + </div> |
| 64 | + ); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +const mapStateToProps = state => { |
| 69 | + return { isLoggedIn: state.firebase.auth.uid }; |
| 70 | +}; |
| 71 | + |
| 72 | +export default compose(firebaseConnect(), connect(mapStateToProps))(PageLogin); |
0 commit comments