So, I have a User model which implements IdentityInterface.
class User extends ActiveRecord implements IdentityInterface
{
...
}
and I another LoginForm model class which call User’s login method.
class LoginForm extends Model
{
...
public function login()
{
if ($this->validate()&&$this->User->validateduser==1) {
return Yii::$app->user->login($this->getUser(), 30);
} else {
return false;
}
}
...
}
I check on whether the user logged-in or not using Yii::$app->user->isGuest. What am I missing?
Hi Stefano, thank you for the prompt response. I did as the doc suggest and I’ve found out that whether I set enableAutoLogin to true or false, it doesn’t set “Yii::$app->user->isGuest” to true.
However, it is working when I add $authTimeout configuration.
<?php
namespace app\models;
use Yii;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\IdentityInterface;
class User extends ActiveRecord implements IdentityInterface
{
public static function tableName()
{
return 'user';
}
public function rules()
{
return [
[['username', 'password'], 'required', 'message' => 'Input cannot be left blank'],
];
}
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'User name',
'password' => 'Password',
];
}
public function getAuthKey() {
return $this->auth_key;
}
public function getId() {
return $this->getPrimaryKey();
}
public function validateAuthKey($authKey) {
return $this->getAuthKey() === $authKey;
}
public static function findIdentity($id) {
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null) {
return static::findOne(['access_token' => $token]);
}
public static function findByUsername($username)
{
return static::findOne(['username' => $username]);
}
public static function findByRegistrationKey($registrationkey)
{
return static::findOne(['registration_key' => $registrationkey]);
}
public static function findByEmail($email)
{
return static::findOne(['email' => $email]);
}
public function validatePassword($password)
{
return Yii::$app->getSecurity()->validatePassword($password, $this->password);
}
public function setPassword($password)
{
$this->password_hash = Security::generatePasswordHash($password);
}
public function generateAuthKey()
{
$this->auth_key = Security::generateRandomKey();
}
public function generatePasswordResetToken()
{
$this->password_reset_token = Security::generateRandomKey() . '_' . time();
}
public function removePasswordResetToken()
{
$this->password_reset_token = null;
}
}