I want to programmatically log in a user with a custom controller. I want to pass my data with $_POST
to the route (I pass data from a mobile application); if username and password are correct, the method/function should return TRUE
.
The route is the following one.
tcdev.login:
path: '/tclogin'
defaults:
_controller: 'Drupal\tcdev\Controller\MYCenterController::login'
_title: 'Login call back'
requirements:
_permission: 'access content'
The code for the controller is the following one.
namespace Drupal\tcdev\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MyController extends ControllerBase {
/**
* \Drupal\user\UserAuth definition.
*
* @var \Drupal\user\UserAuth
*/
protected $userAuth;
/**
* {@inheritdoc}
*/
public function __construct(UserAuth $user_auth) {
$this->userAuth = $user_auth;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('user.auth'));
}
}
public function login() {
$uid = $this->userAuth->authenticate($_POST['user'], $_POST['pass']);
$acc = User::load($uid);
//return new Response('Hi ' . $uid);
}
}
It returns me the following error:
TypeError: Argument 1 passed to
Drupal\tcdev\Controller\MyController::__construct()
must be an instance ofDrupal\tcdev\Controller\UserAuth
, instance ofDrupal\user\UserAuth
given, called in /Users/yms/Sites/devdesktop/gttc/modules/tcdev/src/Controller/MyController.php on line 32 inDrupal\tcdev\Controller\MyController::__construct()
(line 23 of modules/tcdev/src/Controller/MyController.php)
Where is my mistake?