Login from external code

General questions relating to Joomla! 5.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Post Reply
BENKENOBI
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Wed Apr 19, 2023 1:30 pm

Login from external code

Post by BENKENOBI » Wed May 22, 2024 4:29 pm

This code works perfect on Joomla 4:

Code: Select all

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $username = $input->get("username", null, "string");
	$referer = $input->get("referer", base64_encode("./"), "string");
	$password = $input->get("password", null, "string");

        //...

	$app = Factory::getApplication();

	if (!$response){
		$sess->set("adm_message", "Usuario o contraseña incorrectos");
	}
	else{
		try {
			$app = Factory::getApplication('site');
			jimport('joomla.plugin.helper');
			
			$credentials = array();
			$credentials['username'] = $username;
			$credentials['password'] = $password;
			
			$app->login($credentials);
			$user = Factory::getUser();
		}
		catch (Exception $e) {
			echo $e->getMessage();
		}
	}
	
        $app->redirect(base64_decode($referer));
	exit;
}
But on Joomla 5 is not working and i get the following error:

Attempted to load class "Joomla" from namespace "Joomla\Plugin\User\Joomla\Extension".
Did you forget a "use" statement for another namespace?

$response is true, the problem is when i try to login the user.
Last edited by pe7er on Wed May 22, 2024 8:21 pm, edited 1 time in total.
Reason: added code blocks for readability

Post Reply

Return to “General Questions/New to Joomla! 5.x”