0

I have made and fetched a Login form with JWT and react. I managed to fetched the data for the username and password and everything works perfectly but then I want to redirect the user to a new page when he clicks on submit and I have no idea how to do that. I'm also using react router. Any help will be welcome

import React, { Component } from "react";
import axios from "axios";

export default class Login extends Component {
    constructor(props) {
        super(props);
        this.state = {
            username: "test",
            password: "test",
        }
    }

    handleChange = (e) => {
        this.setState({
            [e.target.name]: e.target.value,
        })
    }

    login = () => {
        const {username, password} = this.state;

        axios(`/users/login`, {
          method: "POST", 
          data: {
              username,
              password,
          }
        })
        .then(response => {
            localStorage.setItem('token', response.data.token);
            console.log(response);
        })
        .catch(error => {
            console.log(error)
        })
        
     }

   


    render() {
        return (
            
             
            <div className="log">
                <h3>Sign In</h3>
                
                <div className="form-group">
                    <label>Username</label>
                    <input type="username" className="form-control" placeholder="Enter username" name="username" value={this.state.username} onChange={this.handleChange} />
                </div>

                <div className="form-group">
                    <label>Password</label>
                    <input type="password" className="form-control" placeholder="Enter password" name="password" value={this.state.password} onChange={this.handleChange} />
                </div>

                <div className="form-group">
                    <div className="custom-control custom-checkbox">
                        <input type="checkbox" className="custom-control-input" id="customCheck1"  />
                        <label className="custom-control-label" htmlFor="customCheck1">Remember me</label>
                    </div>
                </div>

                <button type="submit" className="btn btn-primary btn-block" onClick={this.login}>Submit</button>
                <p className="forgot-password text-right">
                    Forgot <a href="#">password?</a>
                </p>
               
            </div>

        
            
        );

}
} 

1 Answer 1

1

import withRouter import {withRouter} from 'react-router-dom';

remove export before class Login class Login extends Component {...

export class at the end of the file: export default withRouter(Login);

and use react-router-dom:

axios(`/users/login`, {
    //...
})
.then(response => {
    localStorage.setItem('token', response.data.token);
    console.log(response);
    this.props.history.push('/url');
})
//...
Sign up to request clarification or add additional context in comments.

1 Comment

awesome that works! then if I want to redirected to another component I just change push('/url')?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.