1

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!

  • 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? – jitendrapurohit 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? – Terry Carter 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. – Terry Carter 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 – Terry Carter 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));
  • 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

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.