WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Create Authentication Cookie Outside of WordPress (8 posts)

  1. Cerebral ideas
    Member
    Posted 3 years ago #

    Question: Is there official documentation on creating an authentication cookie outside of WP?

    Scenario: I am planning on utilizing WP v.3 as a CMS for a company's support documentation website. This support CMS is going to be a piece of a larger network of sub-sites. My client does not want to force the user to log-in more than once, so on initial login to the main site (non-WP), we want to create an authentication cookie that WP will recognizes and allows the user to continue on to the site without having to re-log in.

    With all that being said, I have search low and high for "official" documentation on how to do this with no avail. Although, I have found pieces throughout the Net that seem like some-what viable options (or hacks), but I would rather use an "official" method to accomplish this.

    Can anyone out there point me in the right direction?

    Justin

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 3 years ago #

    Creating the WP cookies is painful and difficult.

    A better way would be to create a WordPress plugin that recognizes the authentication from your other site's cookie, if you have one.

    This is actually quite easy to do, really. The authenticate filter makes it simple:

    add_filter('authenticate','custom_login_check', 100);
    function custom_login_check($user) {
    // check if user is already logged in and just return if he is
    if ( is_a($user, 'WP_User') ) { return $user; } 
    
    // do whatever you have to do to check for authentication here
    // in the end, you need to determine which WP user is logged in
    // then return a WP_user object.
    
    // example: say you figured out that it was user number 123
    $user = new WP_User(123);
    
    // always return the $user from the function argument
    // even if you didn't change it
    return $user;
    }

    That's all there is to it, really. How you implement the authentication check is up to you.

  3. Cerebral ideas
    Member
    Posted 3 years ago #

    Hey Otto, thank you very much for helping me out. You make it seem so easy ;) Would you happen to know if there is a plugin that already does this? I have seen a couple of plugins that deal with authentication, but I am never sure if they handle it in the manner in which I need it.

    If you don't mind, I am going to keep this thread open. This will actually be a teammate of mine's responsibility (I am a front-end developer, so this kind of programming gets over my head), and he might have to ask you a question if you wouldn't mind.

    Thanks again for your wisdom and assistance. Have a good day.

    Justin

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 3 years ago #

    Sure, my own Simple Facebook Connect and Simple Twitter Connect plugins use this same basic method to let you login using Facebook or Twitter credentials.

  5. Cerebral ideas
    Member
    Posted 3 years ago #

    Awesome, thank you Otto! We will look into this very soon. You're a life-saver.

    Justin

  6. vikalp.jain@gmail.com
    Member
    Posted 3 years ago #

    Hi Justin,

    I have exactly the same scenario as you described. Were you able to find a solution to this ? If so please let me know. Will really help me.

    Thanks and regards,
    Vikalp Jain

  7. Cerebral ideas
    Member
    Posted 3 years ago #

    Hey Vikalp, we have not started production yet on this portion of development. The developers on my team feel it is very doable, but I will report back once it is done. Talk to you soon.

    Justin

  8. vikalp.jain@gmail.com
    Member
    Posted 3 years ago #

    Thanks for quick response Justin. Look forward to hearing from you soon.

    Thanks and regards,
    Vikalp Jain

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.