0

Hi I am building a React native app based on my wordpress website so I need to make a registration and login logic to get the user id and user data, thankfully I made the registration logic by creating new user using the rest api, but I need help in making the login thing since I find nothing helpful while searching Google.

I want to post the username or email and the password to authenticate that the user do exist in my site

Ahmed Ali
  • 33
  • 4
  • Does this answer your question? [Register/login user with Wordpress JSON API](https://stackoverflow.com/questions/13679001/register-login-user-with-wordpress-json-api) – Sean H Jun 18 '23 at 12:40

1 Answers1

0
   register_rest_route(
     'custom-plugin', '/login/',
     array(
       'methods'  => 'POST',
       'callback' => 'login',
     )
   );
 }
 
 function login($request){
     $creds = array();
     $creds['user_login'] = $request["username"];
     $creds['user_password'] =  $request["password"];
     $creds['remember'] = true;
     $user = wp_signon( $creds, false );
 
     if ( is_wp_error($user) )
       echo $user->get_error_message();
 
     return $user;
 }
 
 add_action( 'after_setup_theme', 'custom_login' );

Then your API will be created as

http://www.url.com/wp-json/custom-plugin/login
Try it with Postman You will get 200 as a response and user info


body:
{
      "username": ""fakmamail@gmail.com",//or the username
      "password": "t433434533"
    }
Ahmed Ali
  • 33
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 03 '22 at 18:52