Yii2 - AutoLogin not working

Hello,

i have install the Yii2 Basic Version. I expaned the basic version for the login from database. It works fine, but if i visit the page at a future date, i wont automatic logged in though the cookie _identity is available.

My IdentityClass:


namespace app\models\Objects;

class User extends \app\models\ActiveRecord\User implements \yii\web\IdentityInterface 

{

    public static function findIdentity($id) {

        $dbUser = static::find()->where(["id" => $id])->one();

        if (!count($dbUser)) {

            return null;

        }

        return new static($dbUser);

    }


    public static function findByEmail($email){

        return static::findOne(['email' => $email]);

    }


    public static function findIdentityByAccessToken($token, $userType = null) {

        var_dump('Auto LOGIN');

        die();

        return NULL;

    }


    public function getId() {

        return $this->id;

    }


    public function getAuthKey() {

        return NULL;

    }


    public function validateAuthKey($authKey) {

        var_dump('AUTO LOGIN 2');

        return NULL;

    }


    public function validatePassword($password) {

        return $this->password === md5($password);

    }

}

I give back NULL on findIdentityByAccessToken, because i dont need this. In my Database is currently only email, password and id available. In some methodes i added var_dump, because i want to see, if they called by yii. But they doesnt called.

My config cointans in components following:


'user' => [

            'identityClass' => 'app\models\Objects\User',

            'enableAutoLogin' => true,

            'loginUrl' => [ 'user/login' ],

],

My Login Form Model:


namespace app\models\Form;

use Yii;

use yii\base\Model;

use app\models\Objects\User;

class Login extends Model 

{

    public $email;

    public $password;

    public $rememberMe = false;

    private $_user = false;


    public function rules() {

        return [

            [['password', 'email',], 'required'],

            ['email', 'email'],

            ['rememberMe', 'boolean'],

//            ['verifyCode', 'captcha'],

            ['password', 'validatePassword'],

        ];

    }


    public function attributeLabels() {

        return [

            'verifyCode' => 'Verification Code',

        ];

    }


    public function validatePassword($attribute, $params) {

        if (!$this->hasErrors()) {

            $user = $this->getUser();

            if (!$user || !$user->validatePassword($this->password)) {

                $this->addError($attribute, 'Incorrect username or password.');

            }

        }

    }


    public function login() {

        if ($this->validate()) {

            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);

        } else {

            return false;

        }

    }


    public function getUser() {

        if ($this->_user === false) {

            $this->_user = User::findByEmail($this->email);

        }

        return $this->_user;

    }

}

Any my LoginAction:


namespace app\controllers\User;

use Yii;

use app\models\Form\Login;

use yii\base\Action;


class LoginAction extends Action{


    public function run(){

        if (!\Yii::$app->user->isGuest) {

            return $this->controller->goHome();

        }

        $model = new Login();

        if ($model->load(Yii::$app->request->post()) && $model->login()) {

            return $this->controller->goBack();

        } else {

            return $this->controller->render('login', [

                'model' => $model,

            ]);

        }

    }


}

Is the problem here?

Best regards

I’ve got a similar problem, I think there is some code missing that checks the cookie value and logs in the user. So far I haven’t found any documentation about this. Someone please enlighten us.

Help me please. I’ve got a similar problem.

you can change name of session in config/main.php

 'session' => [
        // this is the name of the session cookie used for login on the backend
        'name' => 'name-your-session',
    ],

if this don’t work, you can set true on enabledAutoLogin