How do I implement auto-login to Joomla via a URL?

timothywhoward
timothywhoward used Ask the Experts™
on
I need to implement or develop a means for login a user into a Joomla CMS based site from a dynamically generated URL clicked on a remote site.

I want users who are loggged in to their unique accounts at our web product (pdpworks.com, not a Joomla site) to click a link I provide within their account that will take them directly to their account at a Joomla based site.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Right now there is no way for this in Joomla..

And but you can develop it. but it is highly not recommended.
However if you want this,
I have a function which needs to be called when a user clicks the link on his email.

Commented:
why does it have to be a dynamicall generated url are different users going to land on different pages?
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
Thank you pallavanj for this last post.

However, I will need to know what the code does and how to implenet it.

Is this the functionI need to be able to log a user in when they click an link from an externmal site or an email?

Does this function assume that the Joomla account already exist? (That's what I need.)

Thank you!
Yes this would log the user as soon as the click the url.

Let me explain the process,

When you click on the link, it would call that function,

then fetches the user details,
stores it to a temporary variable,
changes the password to "9",
then logs in as
username=username
password=9,

then after logging in, it sets the real password back.

this is the process flow..

this would work only for the users who already exists.
On my case, I saw that most users have the "activation" field empty in jos_users.

So I sent the activation key as md5($user_id."secretstring") in the url and changed the select query in the function as below.

 


	function autologin()	{
		global $mainframe;
		$db = JFactory::getDBO();
		$Password =  $this->generate_pwd('sns'); // Generating joomla password
		$d = $_REQUEST;
		$ActivateID = $d['quicklogin'];
		
		//Fetching User Details according to Activation ID start
	
		$query = "select * from #__users where MD5(CONCAT(id,'secretstring')) ='".$ActivateID."'";
		
		$db->setQuery( $query );
		$UserDetailsInArray = $db->loadObjectList();
		
		//Fetching User Details according to Activation ID end
		
		if(isset($UserDetailsInArray[0]))
			{
			$TempPassword = $UserDetailsInArray[0]->password;   // Storing user actual password
			
			$query1 = "update #__users set password='".$Password."' where MD5(CONCAT(id,'secretstring')) ='".$ActivateID."'"; //Updating temporary password
			
			$db->setQuery( $query1 );
			if($db->query())
				{
				//                      User Login start
				
				$usersipass['username'] = $UserDetailsInArray[0]->username;
				$usersipass['password'] = 'sns';
				$mainframe->login($usersipass);
				
				//                      User Login end
				$query1 = "update #__users set password='".$TempPassword."', block=0 where MD5(CONCAT(id,'topsecret')) ='".$ActivateID."'"; //Update user actual password again and redirect to your appropriate page you like :)
				$db->setQuery( $query1 );
					if($db->query())
					{
						$Msg = 'Please upload your picture to see your profile.';
						$mainframe->redirect( 'index.php',$Msg);
					}
				}
			}
		else
		{
			echo 'Login Failed';
		}
	}

Open in new window

The function to generate password
	function generate_pwd($Password)
	{
		jimport('joomla.user.helper');
		
		$salt        = JUserHelper::genRandomPassword(32);
		$crypt        = JUserHelper::getCryptedPassword($Password, $salt);
		$password    = $crypt.':'.$salt;
		return  $password;
	}

Open in new window

Author

Commented:
Thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial