rafaelhz / react-material-admin-template

A simple responsive admin template using react and material-ui
633 stars 269 forks source link

How can I redirect page after signin #24

Closed kamalpanhwar closed 5 years ago

kamalpanhwar commented 5 years ago

I am using this theme, but I have no idea how can I redirect my user to other page after signin? or if user is not authenticated how can I redirect to sign. I tried many methods but could not work out. Example

handleSubmit (event) {
    event.preventDefault();
    const email = document.getElementById('email').value;
    const password = document.getElementById('password').value;
    const request = {"auth": {"email": email, "password": password}};
    post('/api/user_token', request)
      .then(response => {
        localStorage.setItem("jwt", response.data.jwt);
        this.props.history.push("/");
      })
      .catch(error => console.log('error', error));
  }      

I also tried following code

return <Redirect to={{ pathname: '/login', state: { from: props.location } }} />

It gave error and not working.

kamalpanhwar commented 5 years ago

I solved it by using other codes and browserHistory.

authenticationService.login(email, password)
        .then( user => {
           const { from } = this.props.location.state || { from: { pathname: "/" } };
          browserHistory.push(from);·
        },
          error => {
--            return <Redirect to='/login' />,
              console.log(error);
          }
        );