1

I hope you can help me with this to find a solution.

On site1 I have a Joomla 3.x installation. I want to run an external script which validates a user on site2 (this is already working).

After getting a positive response from site2, I get the username passed back to the Joomla site1 script (or if needed a second script on site1).

Now what I need to do is to check on site1 if this username exist in the Joomla DB. If true then the user should get logged in into Joomla and redirected to a URL. If not the user should be created in Joomla with a random password and then logged in, too.

I tried the authentication plugin tutorial, but this did not help me. Also I tried this script, but it did not work too: https://gist.github.com/AdamMadrzejewski/020c4fa4b1d0e7af78b8

Any ideas how to log in a user into Joomla from an external script?

Thank you!

1
  • To make your question less Broad, and therefore easier to answer, please show us the code behind your closest attempt and explain where it is success and unsuccessful. It will likely be easier for us to provide a small repair rather than a complete build from scratch. Jul 19, 2018 at 23:01

1 Answer 1

1

This is a pretty late reply but may help someone.

If you want to login user from the Joomla itself as the Site 1 is Joomla you can get the user id of the user and use the following method to login if user id exists else you will first need to register the user using the register model.

function _forceLogin($userId)
{
    $user = JFactory::getUser($userId);
    if ($user->guest) {
        return 'guest';
    } else {
        //Will authorize you as this user.
        JPluginHelper::importPlugin('user');
        $options = array();
        $options['action'] = 'core.login.site';
        $response = new stdClass();
        $response->username = $user->username;
        $response->language = '';
        $response->email = $user->email;
        $response->password_clear = '';
        $response->fullname = '';
        $result = JFactory::getApplication()->triggerEvent(
            'onUserLogin', array((array)$response, $options)
        );
        return $result;
    }
}

This is the only best way to login as if you don't use this your user will get logged in but the session for user will not be updated.

Also if you have found a better way please share here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.