WordPress.org

Ready to get started?Download WordPress

Forums

JSON API
[resolved] How to login, register and logout user? (14 posts)

  1. monkeypunch3
    Member
    Posted 6 months ago #

    Is there a controller to login, register and logout the user?

    http://wordpress.org/plugins/json-api/

  2. monkeypunch3
    Member
    Posted 5 months ago #

    I've created an authentication class to do this. I've sent it to the plug-in author rather than post it here since it is quite long. If anyone would like it I can post it here or email it.

  3. If you want to share the pastebin.com link to that source then please do. But do not encourage people to post email addresses in these forums.

  4. monkeypunch3
    Member
    Posted 5 months ago #

    I've created a pastebin here. I've tested it and it has worked for me. Your mileage may vary. Please post your comments or suggestions. Hopefully it will be integrated into the main branch and if need be, we can work out any bugs. It doesn't support multisite (didn't have time) so that's one thing that can be done.

  5. po2368016
    Member
    Posted 5 months ago #

    Can you post, documentation or examples on how to use your User Controller. I'm really new to this and trying to test things up. Right now I'm just using curl to test everything and makes sure it works.

  6. monkeypunch3
    Member
    Posted 5 months ago #

    Sure. You will want to go through documentation on the main plugin to get a feel for how it works. Once you do that the following will make more sense.

    BTW I'm only a beginner at curl but I'll try to give you a few examples.

    The main thing is to know what information to pass to the API. It will either be all in a GET request (in the URL query) or if more info is needed, in the POST data.

    When making a call the Query is appended to the URL to your WP install. So, if your site is at http://www.mysite.com/blog/ then the query would be appended as, "http://www.mysite.com/blog/?json=user/login".

    LOGIN
    Query:
    ?json=user/login

    The POST information should include:
    log = string
    pwd = string
    rememberme = true or false

    REGISTER
    Query:
    ?json=user/register

    POST data:
    username = string
    email = string

    LOST PASSWORD
    Query:
    ?json=user/lost_password

    POST data:
    username = string

    RESET PASSWORD / CONFIRM REGISTRATION
    Query:
    ?json=user/reset_password&action=resetpass&key=" + key + "&login=" + username

    POST data:
    pass1 = string
    pass2 = string

    The user would have received the key in an email when they lost their password or registered for the first time. You must then include their new password in the post.

    LOGOUT
    Query:
    ?json=user/logout"

    IS USER LOGGED IN
    Query:
    ?json=user/is_user_logged_in"

    GET LOGGED IN USER
    Query:
    ?json=user/get_logged_in_user"

    CHANGING THE EMAIL ADDRESS SENT TO THE USER
    When you reset your password or register you or your user receive an email from "wordpress@mysite.com". You can change by uncommenting the lines at the top of the User controller and filling them in with the email and name you want to use. You may have to actually create that mailbox on your server depending on your host.

    /** changing default wordpress email settings. uncomment to set your own email */
    add_filter('wp_mail_from', 'new_mail_from');
    add_filter('wp_mail_from_name', 'new_mail_from_name');

    function new_mail_from($old) {
    return 'contact@yoursite.com';
    }
    function new_mail_from_name($old) {
    return 'Your Site';
    }

    It would be nice to have this on the settings page but I ran out of time and you would have to modify the main class which I want to avoid since it is out of my control.

    NOTE: Of course, if your site uses HTTPS rather than HTTP then make your calls are using that.

    Also, when logging in, WordPress creates a set of authentication cookies. I'm not sure if curl supports cookies so it may or may not work.

  7. po2368016
    Member
    Posted 5 months ago #

    Thanks for this really great instructions,

    Just another question, I'm using the user-friendly permalinks, so I'm doing the login like this: http://www.mysite.com/blog/api/user/login/?log=username&pwd=pass but its not working. I'm getting an invalid username.

    And is it possible to login while creating a post?

    Like in one go, Lets say, http://www.mysite.com/blog/api/post/create_post/?dev=1&nonce=??&title=test&status=publish - and insert the login credentials somewhere there?

    Thanks again for answering my question and I really hope you answer this too. :)

  8. monkeypunch3
    Member
    Posted 5 months ago #

    You can't do both at the same time. You will have to log in first.

    The data that you include in the URL is called query data and is part of a GET method type. For login you must send the login information with a POST method type. I'm not sure what you would change to make it work in cURL but the URL would be "http://www.mysite.com/blog/api/user/login/" and then you'd pass the login info via post.

  9. monkeypunch3
    Member
    Posted 5 months ago #

    FYI I've uploaded the project to github to make it easier to work on,
    https://github.com/monkeypunch3/JSON-API-Plus.

  10. po2368016
    Member
    Posted 5 months ago #

    Another question, and this is quite out of topic. I'm really having trouble using the custom_fields arguments, lets say I have a custom field : price. and I want to add a value to price while I create a post?

    Thanks in advance, I created a similar post here but didn't get any reply, mr. monkeypunch3 is active here. I might as well ask it here.

    Thanks in advance,

    PS: I saw this while searching for authentication stuff for json api. https://github.com/Achillefs/wp-json-api

    and he's controller can include login credentials during create posts.

    But I think monkeypunch's controller is better due to security details.

  11. monkeypunch3
    Member
    Posted 5 months ago #

    What? That's this plugin. You posted a link to the github repository.

    With the user controller I wrote above there's no way to do both at the same time. You are free to add that functionality in and send me the patch?

    As setting custom fields search this forum. I don't have the time to find it right now but there's a patch that allows you to set the value. Here is the code you need to add.

    In models/post.php after this code:

    if (isset($wp_values['ID'])) {
    	$this->id = wp_update_post($wp_values);
    } else {
    	$this->id = wp_insert_post($wp_values);
    }
    

    add:

    // add custom fields
    if ( !empty($values["custom"]) ) {
    	foreach ($values["custom"] as $metakey => $metavalue) {
    		update_post_meta($this->id,$metakey, $metavalue);
    	}
    }
    
  12. po2368016
    Member
    Posted 5 months ago #

  13. patrickstar
    Member
    Posted 5 months ago #

    Sir, Can you provide a complete sample of the query?

    I'm using my browser to test and I'm really having trouble logging in and registering. I always get Invalid Username or Username is Empty.

    Can you please help?

  14. monkeypunch3
    Member
    Posted 5 months ago #

    Grasshopper, to really understand how to use this library you need to understand HTTP protocol.

    http://www.google.com/search?q=what+is+the+difference+between+post+and+get

    Your browser can submit GET requests through the URL address bar but not POST requests. You have to create a form to do that and set it to post.

    http://www.programmerinterview.com/index.php/general-miscellaneous/html-get-vs-post/

Reply

You must log in to post.

About this Plugin

About this Topic