1

I'm trying to redirect the user to the Profile page if the login is successful.

I've tried using the React router redirect as so:

signInWithEmailAndPassword = (username, password) => {
  return new Promise((resolve, reject) => {
    axios.post(process.env.REACT_APP_BACK_END_DOMAIN + urls.LOGIN, {
      username,
      password,
    },
    {
      headers: {
        'X-CSRFTOKEN': Cookies.get("csrftoken"),
      }
    }).then(response => {
        console.log(response);
        if(response.status =='200'){
          return <Redirect to='/profile' />
        }
    });
  });
};

But when the login is successful nothing happens.

I don't know what am I doing wrong.

ravibagul91
  • 20,072
  • 5
  • 36
  • 59
Octavian
  • 623
  • 2
  • 6
  • 17

2 Answers2

2

If you are using React Router 4, then you need to use push method on history object or write inside render function

import history from '../history';

history.push('/profile');
tarzen chugh
  • 10,561
  • 4
  • 20
  • 30
0

I achieved this in following way.

1) take a state value like this.state = {redirectStatus : false}

2) as you are checking status code, set the state to true like so this.setState({redirectStatus :true})(if status code is 200)

3) finally you can do some thing like this outside the promise

if(redirectStatus ) {
return <Redirect to='/profile' />
}
Guruprasad
  • 753
  • 6
  • 23