20

I want to create mobile app for one wordpress website. I have integrated the wordpress json plugin. I'm not sure where I can find service for user registration and login. Please advice.

narek.gevorgyan
  • 4,165
  • 5
  • 32
  • 52
  • Check out https://github.com/mattberg/wp-json-api-auth. It will handle user login, but not registration. It's been working well for me in a native app I'm building. – jasonlcrane Jan 04 '13 at 15:21

5 Answers5

34

1.Paste Following code in your themes function.php file.

2.Make sure that WP-REST-API plugin Should be installed on wordpress site

add_action( 'rest_api_init', 'register_api_hooks' );

function register_api_hooks() {
  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

willJk
  • 111
  • 2
  • 4
  • 12
Ganesh Hargude
  • 377
  • 3
  • 2
  • This is the easiest way i saw over all answers i found.. thanks man. – Biskrem Muhammad Dec 08 '17 at 20:28
  • though it give me response in browser but where ever i make an http request from android, or even php, script doesn't see any data, android says: FileNotFoundExeption – Biskrem Muhammad Dec 09 '17 at 15:00
  • 2
    I'm not sure that the last add_action is necessary. – Nathan Moinvaziri Aug 29 '18 at 12:40
  • Hi, I am getting this error with status code 404 - rest_no_route. Please help me – AndiM May 17 '19 at 06:30
  • This is spectacular, thank you! Can you also login users with their email? – Jonathan Tuzman May 28 '19 at 20:38
  • @Ganesh Hargude: Can you please help me with how to create WooCommerce Forgot Password API? By using your above example I have created Login API and that working well. – Ketan Nov 05 '19 at 07:01
  • I would like to point out that wp_sigon() function will auto select to use secure cookie or not, I would suggest to set the $user variable as such $user = wp_signon($creds); – Tony Jan 01 '20 at 00:46
  • For login information with passwords you want to use POST not GET – willJk Feb 20 '20 at 19:48
  • Thank you so much for this! How do I make a call to this, is it secure to just send the password in an AJAX call from a remote server? – Sean H Jun 18 '23 at 12:38
12

I was able to figure out both login and signup using @Salam El-Bannas' link, in case any one still needs this here you go:

All through you need two plugins to get the job done:

WordPress JSON API plugin

and

JSON API User

  1. For registration of users: You need nonce ID which will be a part of the registration params in the GET url process (this is provided by the plugins). Read @Salam El-Bannas' link for more understanding. The methodology i used in android was that immediately the page loads I block the UI with "Connecting..." message while in the background am getting the nonce ID once its done the UI Blocking disappears and the nonce ID is added to a special non-editable EditText field created for the nonce ID, the user then enters his/her required credentials and I authenticate that its valid before connecting to the server using the nonce ID i got. the result from the server is JSON so i can handle the rest in android code using volley and GSON.

  2. For Login of users: You only need cookies ID generated by the plugin eg in my case http://localhost/mylocalhost/my_api_base/user/generate_auth_cookie/?insecure=cool&email=xxx@xdfer.org&password=xxxv678

and the result was this json;

{"status":"ok","cookie":"xxxx|1486130938|Ot6yAX7iU773JnQ2zfE8sdmjt09LhHqDKSYBqtekuha|7fe58a35ab9f260c9bced9148f5cf9ae3ab56c16d7d9ce3b2db7da651d4d937d","cookie_name":"wordpress_logged_in_4132d8131ebbc6760d21627637bd4b20","user":{"id":1,"username":"administrator","nicename":"administrator","email":"xxx@xdfer.org","url":"","registered":"2016-11-02 17:46:19","displayname":"xxxx","firstname":"","lastname":"","nickname":"xxxxwedds","description":"","capabilities":"","avatar":null}}

then you have to read this nice tutorial to use the resulting JSON in android (that's if you are new to android login) else just use your discretion.

Its really really really simple and interesting once you follow my lined up process.

The Billionaire Guy
  • 3,382
  • 28
  • 31
  • the link of [teachattitude](http://techattitude.com/tips-tricks-and-hacks/how-to-remotely-create-a-user-on-your-wordpress-website-using-json-api-request/) seems to be broken – ganjaam Jul 15 '22 at 17:09
  • 1
    @ganjaam majorly the content of the link has been extracted in my answer above, the link is an indepth explanation to remote user creation using wordpress api. – The Billionaire Guy Jul 16 '22 at 07:40
9

To register a user this will show you exactly how to register one on the database by simply calling a url and adding data to it using GET Method. Now to do so from a mobile app you just have to make an http request to a url containing all the data required for the user. This will show you how to make a request from Android.

This is just for registering users there will be another plugin JSON APi Auth used in order to login a user.

These are the basics since I don't have much time now, when I do, I will provide full details and example. But for now this shall do it

Community
  • 1
  • 1
Salam El-Banna
  • 3,784
  • 1
  • 22
  • 34
1

Use with clean Class

// Register REST API endpoints
class Login_REST_API_Endpoints {

    /**
     * Register the routes for the objects of the controller.
     */
    public static function register_endpoints() {
        // endpoints will be registered here
        register_rest_route( 'wp', '/login', array(
            'methods' => 'GET',
            'callback' => array( 'Login_REST_API_Endpoints', 'login' ),
        ) );
    }

    /**
     * @param WP_REST_Request $request Full data about the request.
     * @return WP_Error|WP_REST_Request
     */
    public static function login( $request ) {


        $data = array();
        $data['user_login'] = $request["username"];
        $data['user_password'] =  $request["password"];
        $data['remember'] = true;
        $user = wp_signon( $data, false );

        if ( !is_wp_error($user) )
          return $user;
    }

}
add_action( 'rest_api_init', array( 'Login_REST_API_Endpoints', 'register_endpoints' ) );
WebMat
  • 126
  • 4
0

Try WP REST User it has create user and reset password functionality

And for login if you need advanced control over user capabilities use AAM

Mozart
  • 2,117
  • 2
  • 20
  • 38
  • 1
    This plugin has been closed as of July 13, 2021 and is not available for download. Reason: Guideline Violation. – Sean H Jun 18 '23 at 12:36