2

I have a userId - $uid.

I wanted to make this user as logged in to the s/m in joomla.

I've tried this -

$currentUser = new JUser($uid);

$mainframe = JFactory::getApplication('site');
$credentials = array();
$credentials['username'] = $currentUser->username;
$credentials['password'] = $currentUser->password; //this is the hashed password

//perform the login action
$mainframe->login($credentials);

$newuser = JFactory::getUser(); //this doesn't get updated to the new user ($uid) details

How can I make the user as the current user by using the uid itself?

Update: I just found that user can be loaded with JUser::load()

Trial 2-

$user = JUser::getInstance($uid);
$user->load($uid); //this should load the new user.

$newuser = JFactory::getUser(); //still i get the anonymous details here 
            //($newuser->id = 0)

As there is wp_set_current_user($uid); for wordpress user_load_by_name() in drupal, is there any similar function for joomla ? Please help.!!!

Thanks!

8
  • Note - If I add the hardcoded password to $credentials['password'], it works. But I can't hardcode it, can i get the unhashed passwd from the hashed one? Feb 22 '16 at 13:10
  • 1
    It is a little bit vague what you are trying to accomplish. Can you explain your workflow a little bit better? Are you trying to have a default logged in use? Do your users log in to your site using a login form? Can you tell us what "s/m" is? Feb 22 '16 at 16:21
  • 1
    cross-posted on stackoverflow.com stackoverflow.com/questions/35554014/…
    – Joomler
    Feb 22 '16 at 18:51
  • 1
    Pass the username and password, form token to the com_users login controller then and let Joomla "login" the user for you. Using this method you will essentially be logging in the user without actually filling out a form. Feb 23 '16 at 17:32
  • 1
    See this post stackoverflow.com/questions/2075335/… It is almost a duplicate of yours, it just needs to be updated to use JInput instead of JRequest Feb 23 '16 at 17:36
2

You can simply do this by using a core plugin...

//Will obtain user object - what the question author needed.
$user = JFactory::getUser($uid);

//Will authorize you as this user.
JPluginHelper::importPlugin('user');
$options = array();
$options['action'] = 'core.login.site';
$response->username = $user->username;
$result = $app->triggerEvent('onUserLogin', array((array)$response, $options));
2
  • 1
    Not sure if you can really login user this way, but I believe that first line of code is the answer that was issued. You might need to create custom plugin to login user without password.
    – Alexandr
    May 24 '16 at 20:16
  • Sure you can login with this, I have tested
    – Anant
    Dec 13 '17 at 15:34
1

This may no have been available back then, but now I use two ways of loading users in Joomla.

Method 1: Load any user as an instance of the User class

Loading a user this way will simply store it in the variable and you can then use it for other things. This does not affect the current session and cannot make you login as a user.

<?php
$foo = JFactory::getUser(1234)

Method 2: Load any user as currently logged in user

<?php
JFactory::getUser()->load(1234)

After issuing this command, Joomla with see user 1234 as logged in. In fact, any subsequent call to JFactory::getUser() will return an instance of the User class for user id 1234

Edit: If you are using this in a loop, it will help if you force Joomla to logout first by doing this ...

<?php
// First, logout ...
Factory::getApplication()->logout()
// ... then load the account.
JFactory::getUser()->load(1234)

I have seen a case where the loop kept remembering the first loaded item. It's probably due to a race condition somewhere in Joomla.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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