Yii2 login from database
- Create users table
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(255) DEFAULT NULL,
`password_hash` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `email` (`email`) USING BTREE
);
-
Create Users Model
While you are at Gii, you should create search model too.
-
Add the following namespaces in Users model class
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\IdentityInterface;
-
Implements the IdentityInterface interface class with Users model class
class Users extends \yii\db\ActiveRecord implements IdentityInterface
Then add all the inherited functions from yii\web\IdentityInterface. See https://www.yiiframework.com/doc/api/2.0/yii-web-identityinterface
Add function validatePassword in model/Users.php.
/**
* Validates password
*
* @param string $password password to validate
* @return bool if password provided is valid for current user
*/
public function validatePassword($password)
{
return password_verify($password, $this->password_hash);
}
You can change the code to whatever you use to validate the password.
- Assign identityClass in config\web.php
'user' => [
'identityClass' => 'app\models\Users',
'enableAutoLogin' => false,
],
-
Modify models\LoginForm to use email as login and Users model. Just do a global replace in models\LoginForm to replace username with email.
For function getUser, change the model to Users and fix the capitalization. And add function findByEmail in models/Users.php.
/**
* Finds user by email
*
* @param string $email
* @return static|null
*/
public static function findByEmail($email)
{
return static::findOne(['email' => $email]);
}
-
Update views/site/login.php and do global replace to change username to email
- Update views/layout/main.php and change 'Logout (' . Yii::$app->user->identity->email . ')' to 'Logout (' . Yii::$app->user->identity->email . ')'. This will update the logout menu to use email field instead of username.
- ['email', 'email'] in models/LoginForm
- ['email', 'email'] and ['email', 'unique'] in model/Users
Completed models\Users.php
<?php
namespace app\models;
use Yii;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\IdentityInterface;
/**
* This is the model class for table "users".
*
* @property string $id
* @property string $email
* @property string $password_hash
*/
class Users extends \yii\db\ActiveRecord implements IdentityInterface
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'users';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['email', 'password_hash'], 'string', 'max' => 255],
['email', 'email'],
['email', 'unique'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'email' => 'Email',
'password_hash' => 'Password Hash',
];
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->authKey;
}
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return bool if password provided is valid for current user
*/
public function validatePassword($password)
{
return password_verify($password, $this->password_hash);
}
/**
* Finds user by email
*
* @param string $email
* @return static|null
*/
public static function findByEmail($email)
{
return static::findOne(['email' => $email]);
}
}
models/LoginForm.php
<?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 $email;
public $password;
public $rememberMe = true;
private $_user = false;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// email and password are both required
[['email', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
['email', 'email'],
];
}
/**
* 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 email or password.');
}
}
}
/**
* Logs in a user using the provided email 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 : 0);
}
return false;
}
/**
* Finds user by [[email]]
*
* @return User|null
*/
public function getUser()
{
if ($this->_user === false) {
$this->_user = Users::findByEmail($this->email);
}
return $this->_user;
}
}
Credit: https://www.compromath.com/code-formatting-syntax-highlighter-for-blogger


Comments
Post a Comment