I have a problem with logging in Yii2.
I created a database with table named users
and corresponding model Users
using Gii.
I also changed config/web.php
file:
'user' => [
'identityClass' => 'app\models\Users',
'enableAutoLogin' => true,
],
Here is my app\models\Users
:
namespace app\models;
/**
* This is the model class for table "users".
*
* @property integer $id
* @property string $username
* @property string $email
* @property string $password
*/
class Users extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface {
/**
* @inheritdoc
*/
public static function tableName() {
return 'users';
}
/**
* @inheritdoc
*/
public function rules() {
return [
[['username', 'email', 'password'], 'required'],
[['username', 'email'], 'string', 'max' => 200],
[['password'], 'string', 'max' => 300],
];
}
/**
* @inheritdoc
*/
public function attributeLabels() {
return [
'id' => 'ID',
'username' => 'Username',
'email' => 'Email',
'password' => 'Password',
];
}
public static function findByUsername($username) {
return self::findOne(['username' => $username]);
}
public function validatePassword($password) {
return $this->password === $password;
}
public static function findIdentity($id) {
$user = Users::findOne($id);
if (count($user)) {
return new static($user);
}
return null;
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null) {
throw new \yii\base\NotSupportedException();
}
public function getAuthKey() {
return $this->authKey;
}
public function validateAuthKey($authKey) {
return $this->authKey === $authKey;
}
public function getId() {
return $this->username;
}
}
And here is my SiteController
code in actionLogin()
:
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
//var_dump(Yii::$app->user->identity);
return $this->goBack();
//return $this->render('login',Yii::$app->user->identity);
}
return $this->render('login', [
'model' => $model,
]);
}
This is when redirect to home not show username and logout but when I use:
var_dump(Yii::$app->user->identity);
It shows all variable set true.
and here is loginform
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* LoginForm is the model behind the login form.
*
* @property User|null $user This property is read-only.
*
*/
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user = false;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
/**
* Logs in a user using the provided username and password.
* @return bool whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 20);
}
return false;
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
public function getUser()
{
if ($this->_user === false) {
$this->_user = Users::findByUsername($this->username);
}
return $this->_user;
}
}
I created same program before and works fine. Where is the problem? Any way to solve this?