Last active
June 10, 2017 14:27
-
-
Save stepankuzmin/b2ec8e4d75419be236ce1a4db259aea4 to your computer and use it in GitHub Desktop.
authentication with react-router-redux 5.x and redux-saga https://medium.com/@stepankuzmin/authentication-with-react-router-redux-5-x-and-redux-saga-55da66b54be7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { PureComponent } from 'react'; | |
| import PropTypes from 'prop-types'; | |
| import { connect } from 'react-redux'; | |
| import { Redirect } from 'react-router-dom'; | |
| import { authorize } from './reducer'; | |
| import { tokenSelector, errorSelector } from './selectors'; | |
| class Login extends PureComponent { | |
| constructor(props) { | |
| super(props); | |
| this.state = { login: '', password: '' }; | |
| this.onChange = this.onChange.bind(this); | |
| this.onSubmit = this.onSubmit.bind(this); | |
| } | |
| onChange(input, value) { | |
| this.setState({ [input]: value }); | |
| } | |
| onSubmit() { | |
| const { login, password } = this.state; | |
| this.props.dispatch(authorize(login, password)); | |
| } | |
| render() { | |
| const { error, token } = this.props; | |
| if (token) { | |
| return <Redirect to="/" />; | |
| } | |
| return ( | |
| <div> | |
| <input | |
| type='text' | |
| placeholder='login' | |
| value={this.state.login} | |
| onChange={this.onChange.bind(this, 'login')} | |
| /> | |
| <input | |
| type='password' | |
| placeholder='password' | |
| value={this.state.password} | |
| onChange={this.onChange.bind(this, 'password')} | |
| /> | |
| <button onClick={this.onSubmit}>Submit</button> | |
| </div> | |
| ); | |
| } | |
| } | |
| const mapStateToProps = (state) => ({ | |
| token: state.auth.token, | |
| error: state.auth.error | |
| }); | |
| export default connect(mapStateToProps)(Login); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment