18

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,067
  • 5
  • 30
  • 51
  • 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

33

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
  • 367
  • 3
  • 2
11

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
  • 2,992
  • 27
  • 29
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,436
  • 1
  • 19
  • 32
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
  • 96
  • 4
1

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 AlKhateeb
  • 1,943
  • 2
  • 17
  • 34