1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to log users in? (SSO)

Discussion in 'XenForo Development Discussions' started by Robust, Apr 19, 2016.

  1. Robust

    Robust Well-Known Member

    Hey,

    Back after a while...

    How do you log users in XenForo? I plan to have this kind of setup:
    mysite.com
    auth.mysite.com
    forums.mysite.com
    [...]

    The domain auth.mysite.com manages authentication and logs users into all services of mysite.com. This has been implemented pretty well, just need to implement it into XenForo also. How can it be modified to log users into XenForo also? Do I just create some sort of cookie or?

    I'm not sure if it's possible to initialise XenForo's bootloader and all in here, by the way. So if possible, another method would be preferred. If that's the absolute only way, then I can try and figure something out.

    Edit: For clarification, I do not want my application to replace XenForo's users system as such. I want XenForo to manage the users as it already does, just the login/authentication part to be centralised to the app. I can obviously edit the templates for this to redirect to the central auth site. I just need to know how to log a user in. User fields and all of that XenForo stuff managed in XF already will continue to be managed in XF.
     
    Last edited: Apr 19, 2016
  2. Robust

    Robust Well-Known Member

    Since users will already be authenticated (or, well, I log them in when they authenticate) can I just do this:

    Code:
            $visitor = XenForo_Visitor::setup($userId);
    $userModel->setUserRememberCookie($userId);
    
            XenForo_Application::getSession()->userLogin($userId, $visitor['password_date']);
    Doesn't look right to me, isn't setting any cookies.
     
  3. Arty

    Arty Well-Known Member

    Yes, you can do that. I'm using similar code on styles demo to automatically login visitor as test user. Extend class XenForo_Session, add that code to _setup(). This is my code
    Code:
    <?php
    
    class StylesDemo_Session extends XFCP_StylesDemo_Session
    {
       protected function _setup($sessionId = '', $ipAddress = false, array $defaultSession = null)
       {
         $result = parent::_setup($sessionId, $ipAddress, $defaultSession);
    
         if (!defined('SWITCHED_USER') && StylesDemo_Style::$styleId !== 0)
         {
           define('SWITCHED_USER', true);
    
           if (StylesDemo_Style::$guestMode)
           {
             // Switch to guest
             if ($this->get('user_id'))
             {
               $this->changeUserId(0, true);
             }
             return $result;
           }
           else
           {
             // Switch to test user
             $options = XenForo_Application::get('options');
             $testUser = intval($options->demoTestAccount);
    
             if ($this->get('user_id') != $testUser)
             {
               $this->changeUserId($testUser, true);
             }
             return $result;
           }
         }
    
         return $result;
       }
    }
    I'm using some define() to avoid infinite loop because changeUserId() will redo session, causing _setup() to run again.
     
    PyroM likes this.

Share This Page