How to use WP-REST API to login user and get user data for Android app?

I found the simplest solution using the WP-REST API plugin,first set this in yours environment :

1.) In your themes functions.php register API endpoint hooks:

add_action( 'rest_api_init', 'register_api_hooks' );
// API custom endpoints for WP-REST API
function register_api_hooks() {

    register_rest_route(
        'custom-plugin', '/login/',
        array(
            'methods'  => 'POST',
            'callback' => 'login',
        )
    );

    function login() {

        $output = array();

        // Your logic goes here.
        return $output;

    }

2.) By default, if you have pretty permalinks enabled, the WordPress REST API “lives” at /wp-json/. Then the API endpoint is accessible at youdomain.com/wp-json/custom-plugin/login with a POST request.

Notice that custom-plugin/login is actually defined in register_rest_route in PHP function register_api_hooks()

For API key I am using WordPress Nonces – pretty straightforward as in my discussion here . I hope these answers are useful for all full stack developers who are new to WordPress REST API

Leave a Comment