1

I want when I submit the email and the password to login, it will be redirected to the dashboard, my code is below :

     handleClick(event){
    
    var payload={
      "email":this.state.email,
        "password":this.state.password
    }
    axios({
          method: 'post',
          url: '/app/login/',
          data: payload,
          withCredentials: true,
          headers: {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json',
            'Accept': 'application/json',
          }
        })
   
   .then(function (response) {
     console.log(response);
     if(response.data.code == 200){
          response.redirect('http://localhost:5000/#/dashboard');

     }
     else if(response.data.code == 204){
       swal("Erreur !", "Vérifiez vos champs SVP !", "error");
      
     }
     else{
       swal("Erreur !", "Username inexistant !", "error");
      
     }
   })
  }

My route :

import React from 'react';
import Loadable from 'react-loadable'
const Dashboard = Loadable({
  loader: () =>
    import ('./views/Dashboard'),
  loading: Loading,
});
const routes = [
{ path: '/dashboard', name: 'Dashboard', component: Dashboard }]

When I run it, I get : enter image description here

How can I fix that please ?

8
  • which react router you;re using? Commented Sep 3, 2018 at 17:44
  • please share your code of component and router Commented Sep 3, 2018 at 17:45
  • @SakhiMansoor react-loadable Commented Sep 3, 2018 at 17:45
  • I mean for navigation how are you doing routing ? please share your code Commented Sep 3, 2018 at 17:46
  • @SakhiMansoor I edit my post Commented Sep 3, 2018 at 17:57

2 Answers 2

2

There is no redirect method in axios response object. To handle this you might workout with your router which you use.

Example:

router.push({ path: '/dashboard' }) Like this...

Edited:

In case you using react-router: try to extract history from component props, like this:

this.props.history.push('/dashboard')
Sign up to request clarification or add additional context in comments.

2 Comments

I get : × Unhandled Rejection (TypeError): __WEBPACK_IMPORTED_MODULE_5_react_router__.a.push is not a function
I get Unhandled Rejection (TypeError): Cannot read property 'props' of undefined I edit my post
2

use withRouter to access history props in your component:

here is working example : https://codeshare.io/5OZbdW

Comments

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.