Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- render() {
- if (this.state.redirectTo) {
- return (<Redirect to="/" />);
- } else {
- return ( ///rest of render here
- constructor() {
- super();
- this.state = {
- username: '',
- password: '',
- redirectTo: false
- };
- this.handleSubmit = this.handleSubmit.bind(this);
- this.handleChange = this.handleChange.bind(this);
- }
- handleChange(event) {
- this.setState({
- [event.target.name]: event.target.value
- });
- }
- componentDidMount() {
- this.setState({
- redirectTo: false
- });
- }
- handleSubmit(event) {
- event.preventDefault();
- console.log('handleSubmit');
- axios
- .post('/user/login', {
- username: this.state.username,
- password: this.state.password
- })
- .then((response) => {
- console.log('login response: ');
- console.log(response);
- if (response.status === 200) {
- // update App.js state
- this.setState({
- redirectTo: true
- });
- this.props.updateUser({
- loggedIn: true,
- username: response.data.username,
- id: response.data.id
- });
- // update the state to redirect to home
- }
- console.log(this.state.redirectTo);
- })
- .catch((error) => {
- console.log(this.state);
- console.log('login error: ');
- console.log(error);
- });
- }
Add Comment
Please, Sign In to add comment