Problem With Authentication

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!

This is a very bad impression, why people have not been responding to this question. I have the similar query.

I am not able to login, I tried every other question in the forum but still no success.

Perhaps, but necro-posting makes a bad impression too. Although, the guy complaining about that makes a bad impression too, I guess.

+1 for Ronald.

Concerning the problem, you are comparing lowercase with no lowercase at this line:




$user=User::model()->find('LOWER(username)=?',array($username));



Then, if you type uppercase into the username text field, the comparision fails.

Try:




$user=User::model()->find('LOWER(username)=?',array(strtolower($username)));



Regards.