0

I'm trying to use REST login from a React native app. When the user tries to log in, the app calls:

var data_node = {name:this.state.username,pass:this.state.password};
axios.post('http://IP/user/login?_format=json',data_node)
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error)
  });

If the call succeds, in test, the next time I try to authenticate the user, I get this error because the user has been authenticated in a previous call for which I haven't saved data, so I cannot call user/logout.

This route can only be accessed by anonymous users.

How can I avoid it, forcing a new login?

  • 1
    Because the user already login. You have to logout and test again. – Jonh May 10 at 9:47
  • 1
    Drupal won't be able to help you with this; unless you want to do it the proper way, i.e. logging out before attempting to log in again, you'll have to approach it from the axios point of view, and find a way to stop it sending the session credentials along with the second request. – Clive May 10 at 10:01
0

This issue is only happening because you are trying to access the user/login rest api when you have cookie maintained at your app level. SO, you need to make sure that you clear the cookies before you make a call to login api.

You can use https://github.com/joeferraro/react-native-cookies to make sure that all cookies are cleared before you try login attempt.

Happy coding :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.