When user sign up, system send a confirmation email to user its work good but without any email confirmation system automatic login or user can login. How can i solve this, that user should confirm email before login and if user not confirmed an email user can't be login?
i am using this project: Yii 2 Advanced Template With Rbac User Managment
my LoginForm model code
namespace common\models;
use Yii;
use yii\base\Model;
/**
* Login form
*/
class LoginForm extends Model
{
public $email;
public $password;
public $rememberMe = true;
protected $_user = false;
/**
* @inheritdoc
*/
public function rules()
{
return [
// username and password are both required
['email', 'filter', 'filter' => 'trim'],
[['email','password'], 'required'],
['email', 'email'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword','skipOnEmpty'=>false],
];
}
/**
* 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->$attribute)) {
$this->addError('email', Yii::t('messages','Incorrect password or email.'));
$this->addError('password', Yii::t('messages','Incorrect password or email.'));
}
}
}
/**
* Logs in a user using the provided username and password.
*
* @return boolean 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);
} else {
return false;
}
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
public function getUser()
{
if ($this->_user === false) {
$this->_user = User::findByEmail($this->email);
}
return $this->_user;
}
public function attributeLabels()
{
return [
'email' => Yii::t('app','Email'),
'password' => Yii::t('app','Password')
];
}
}
find below function in common/models/User.php
public static function findByEmail($email)
{
return static::findOne(['email'=>$email,'status'=>self::STATUS_ACTIVE]);
}
and replace it with following
public static function findByEmail($email)
{
return static::findOne(['email'=>$email,'status'=>self::STATUS_ACTIVE,'email_verification_status'=>self::EMAIL_VERIFIED]);
}
Hope this will help you