Skip to main content
[Edit removed during grace period]
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
[Edit removed during grace period]
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
improved grammar and formatting
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

How to login user programmatically Argument 1 must be an instance of Drupal\tcdev\Controller\UserAuth, instance of Drupal\user\UserAuth given

I want to login user programmatically log in a user with a custom controller. I want to pass my data with $_POST$_POST to the route (you suppose II pass data from a mobile application) , then I want; if userusername and pass ispassword are correct return true and else, the method/function should return falseTRUE.

My Router 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'

my Controller 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$this->userAuth = $user_auth;
    }

    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static(
            $container_>get$container->get('user.auth')
        );
    }


 
    }

    public function login() {
        $uid = $this_>userAuth_>authenticate($$this-POST['user']>userAuth->authenticate($_POST['user'], $_POST['pass']);
        $acc = User::load($uid);

        //return new Response('Hi ' . $uid);
    }

}

but it returnIt returns me TypeError: Argument 1 passed to Drupal\tcdev\Controller\MyController::__construct() must be an instance of Drupal\tcdev\Controller\UserAuth, instance of Drupal\user\UserAuth given, called in /Users/yms/Sites/devdesktop/gttc/modules/tcdev/src/Controller/MyController.php on line 32 in Drupal\tcdev\Controller\MyController_>__construct() (line 23 of modules/tcdev/src/Controller/MyController.phpthe following error:

TypeError: Argument 1 passed to Drupal\tcdev\Controller\MyController::__construct() must be an instance of Drupal\tcdev\Controller\UserAuth, instance of Drupal\user\UserAuth given, called in /Users/yms/Sites/devdesktop/gttc/modules/tcdev/src/Controller/MyController.php on line 32 in Drupal\tcdev\Controller\MyController::__construct() (line 23 of modules/tcdev/src/Controller/MyController.php)

whereWhere is my mistake?

How to login user programmatically

I want to login user programmatically with custom controller. I want pass my data with $_POST to route (you suppose I pass data from mobile application) , then I want if user and pass is correct return true and else return false.

My Router

tcdev.login:
  path: '/tclogin'
  defaults:
    _controller: 'Drupal\tcdev\Controller\MYCenterController::login'
    _title: 'Login call back'
  requirements:
    _permission: 'access content'

my Controller

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);
    }

}

but it return me TypeError: Argument 1 passed to Drupal\tcdev\Controller\MyController::__construct() must be an instance of Drupal\tcdev\Controller\UserAuth, instance of Drupal\user\UserAuth given, called in /Users/yms/Sites/devdesktop/gttc/modules/tcdev/src/Controller/MyController.php on line 32 in Drupal\tcdev\Controller\MyController_>__construct() (line 23 of modules/tcdev/src/Controller/MyController.php

where is my mistake?

Argument 1 must be an instance of Drupal\tcdev\Controller\UserAuth, instance of Drupal\user\UserAuth given

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 of Drupal\tcdev\Controller\UserAuth, instance of Drupal\user\UserAuth given, called in /Users/yms/Sites/devdesktop/gttc/modules/tcdev/src/Controller/MyController.php on line 32 in Drupal\tcdev\Controller\MyController::__construct() (line 23 of modules/tcdev/src/Controller/MyController.php)

Where is my mistake?

more details
Link
Yuseferi
  • 22.2k
  • 22
  • 118
  • 218
Loading
added 214 characters in body
Source Link
Yuseferi
  • 22.2k
  • 22
  • 118
  • 218
Loading
added 214 characters in body
Source Link
Yuseferi
  • 22.2k
  • 22
  • 118
  • 218
Loading
edited tags; edited title
Link
Yuseferi
  • 22.2k
  • 22
  • 118
  • 218
Loading
Source Link
Yuseferi
  • 22.2k
  • 22
  • 118
  • 218
Loading