Hi guys, I’m new to Yii framework.
I followed "Building a blog system with yii framework" tutorial. I experienced error when dealing with the authentication. I spent days to find the problem but yet to find one. I hope you guys can help me out. Here is my code :
authenticate function in Useridentity.php component
public function authenticate()
{
$username=strtolower($this->username);
$user=User::model()->find('LOWER(username)=?',array($username));
if($user===null)
{
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
else if(!$user->validatePassword($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$user->id;
$this->username=$user->username;
$this->errorCode=self::ERROR_NONE;
}
return $this->errorCode;
}
public function getId()
{
return $this->_id;
}
This is my validate method in User model (user.php)
public function validatePassword($password)
{
return crypt($password,$this->password)===$this->password;
}
protected function generateSalt()
{
return uniqid('',true);
}
public function hashPassword($password)
{
return crypt($password, $this->generateSalt());
var_dump($this->password);
}
This one is Loginform.php
public function authenticate($attribute,$params)
{
$identity=new UserIdentity($username,$password);
$identity->authenticate();
switch($identity->errorCode)
{
case UserIdentity::ERROR_NONE:
Yii::app()->user->login($identity);
$identity->errorCode;
break;
}
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->username,$this->password);
if($this->_identity->authenticate() == 1)
$this->addError('username','Invalid username.');
else if($this->_identity->authenticate() == 2)
{$this->addError('password','Incorrect password.');
}
}
It return error message “incorrect password”. I can’t find what’s wrong. Thank you for your help!