Question

I have to log a user automatically when certain events happens, example when a user forgot his password and reset it successfully. By client requirements a have to use a file embedded in com_jumi to make the login and not com_user.

I'm using the function login of JSite object as in: $mainframe->login($credentials, $options)
and that method is returning true, then I make a redirect $mainframe->redirect()
but in the landing page there is no user logged in???

Am I missing some step(s) to accomplish this???

Was it helpful?

Solution

I know this is an old post, but since I myself found this post the other day before solving the exact same issue, I decided to post my solution here anyway.

This is the function I created to login (I am not using the remember me options):

function joomla_login($user,$pass) 
{ 
    if ( !$user) return false;
    if ( !$pass) return false;
    $credentials = array( 'username' => $user, 'password' => $pass );
    $login_site =& JFactory::getApplication('site');
    $login_site->login($credentials, $options=array());
    return;
}

OTHER TIPS

//log user in
if(!JFactory::getUser()->id)
{
    $email = (string)$response['linkedin']->{'email-address'};

    $db = JFactory::getDbo();
    $app = JFactory::getApplication();

    $sql = "SELECT * FROM #__users WHERE email = " . $db->quote($email);
    $db->setQuery($sql);
    $result = $db->loadObject();

    if($result->id)
    {
        $jUser = JFactory::getUser($result->id);
        //$userarray = array();
        //$userarray['username'] = $jUser->username;
        //$userarray['password'] = $jUser->password;
        //$app->login($userarray);              

        $instance = $jUser;     
        $instance->set('guest', 0);


        $instance->set('aid', 1);
        $instance->set('usertype', 'Registered');


        // Register the needed session variables

        $session->set('user',$instance);


        // Check to see the the session already exists.                        
        //$app->checkSession();
        //$app->_createSession($session->getId());


        // Update the user related fields for the Joomla sessions table.
        /*$db->setQuery(
                'UPDATE '.$db->nameQuote('#__session') .
                ' SET '.$db->nameQuote('guest').' = '.$db->quote($instance->get('guest')).',' .
                '   '.$db->nameQuote('username').' = '.$db->quote($instance->get('username')).',' .
                '   '.$db->nameQuote('userid').' = '.(int) $instance->get('id') .
                ' WHERE '.$db->nameQuote('session_id').' = '.$db->quote($session->getId())
        );
        $db->query();*/

        // Get the session object
        $table = & JTable::getInstance('session');
        $table->load( $session->getId() );

        $table->guest       = $instance->get('guest');
        $table->username    = $instance->get('username');
        $table->userid      = intval($instance->get('id'));
        $table->usertype    = $instance->get('usertype');
        $table->gid         = intval($instance->get('gid'));

        $table->update();                        

        // Hit the user last visit field
        $instance->setLastVisit();          

        //return true;

        $app->redirect('index.php?option=com_community&view=profile');
    }
    else
    {
        $url = "index.php?option=com_community&view=register";
        $app->redirect($url,'We did not find your email address in our system. Please register.');
        //echo "redirect to registration page";
        //exit();


        //$url = 'index.php?option=com_users&view=registration&name=' . $user_profile['name'] . '&username=' . $user_profile['username'] . '&email=' . $user_profile['email'];
        //$url = JRoute::_($url);
        //$app->redirect($url);
    }
}

I prefer to use User ID, and to get User ID with Jumi in Joomla I do:

defined('_JEXEC') OR defined('_VALID_MOS') OR die( "Direct Access Is Not Allowed" );  

$jAp = & JFactory::getApplication();  
$user = & JFactory::getUser();  
echo $user->get('id');  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top