1

hi i am trying to use yii authentication methods to create a login page for my website . i dont know if i am writing this right. this is my login form:

 <?php $this->beginWidget('WrapViewWidget'); ?>
    <div id="login-header">
        here you can create Question ...
    </div>
    <div class="container">
        <div id="login-content">
            <?php  $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
            'id' => 'login-form',
            'layout' => TbHtml::FORM_LAYOUT_HORIZONTAL,
            'enableClientValidation' => true,
            'clientOptions' => array(
                'validateOnSubmit' => true,
            ),
        )); ?>
        <div class="span5">
            <?php echo $form->labelEx($model, 'username'); ?>
            <?php echo $form->textField($model, 'username',array('class'=> 'form-control'); ?>
            <?php echo $form->error($model, 'username'); ?>
        </div>
        <div class="span5">
            <?php echo $form->labelEx($model, 'password'); ?>
            <?php echo $form->passwordField($model, 'password', array('class' => 'form-control'); ?>
            <?php echo $form->error($model, 'password'); ?>
        </div>
        <div class="span5 rememberMe">
            <?php echo CHtml::activeCheckBox($model, 'rememberMe'); ?>
            <?php echo CHtml::activeLabel($model, 'rememberMe'); ?>
        </div>
        <div>
            <?php echo TbHtml::submitButton('Login', array('class' => 'btn-login')); ?>
        </div>
        <?php $this->endWidget(); ?>
    </div>
</div>
<?php $this->endWidget(); ?>

and this is my userIdentity:

class UserIdentity extends CUserIdentity{
    private $_id;

    public function __construct($username, $password)
    {
        $this->username = $username;
        $this->password = $password;
    }
    public function authenticate()
    {
        $record = User::model()->findByAttributes(array('username' => $this->username));
        if ($record === null)
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        else if(!CPasswordHelper::verifyPassword($this->password,$record->password))
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        else {
            $this->_id = $record->user_id;
            $this->setState('username', $record->username);
            $this->errorCode = self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

    public function getId()
    {
       return $this->_id;
    }

    public function getUsername()
    {
       return $this->username;
    }

    public function getPassword()
    {
        return $this->password;
    }

} 

i have implemented beforeSave in my User model Like this:

public function beforeSave()
{
    if (parent::beforeSave()) {
        $this->password=CPasswordHelper::hashPassword($this->password);
        return true;
    }
    return false;
}

and i have These methods in LoginForm:

public function authenticate($attribute,$params)
{
    $this->_identity=new UserIdentity($this->username,$this->password);
    if(!$this->_identity->authenticate())
        $this->addError('password','username or password is wrong!');
}

public function login()
{
    if ($this->_identity === null) {
        $this->_identity = new UserIdentity($this->username, $this->password);
        $this->_identity->authenticate();
    }
    if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
        $duration = $this->rememberMe ? 3600 * 24 * 30 : 0; // 30 days
        Yii::app()->user->login($this->_identity, $duration);
        return true;
    } else
        return false;
}
public function rules()
{
    return array(
        // username and password are required
        array('username, password', 'required'),
        // rememberMe needs to be a boolean
        array('rememberMe', 'boolean'),

        array('email', 'email'),
        // password needs to be authenticated
        array('password', 'authenticate'),
    );
}

i tried many things and i am sure that i am entering right password. but i dont know where is my mistake . i may have foolish mistake.thank you.

demortz
  • 51
  • 7
  • see - http://stackoverflow.com/questions/20394137/yii-cpasswordhelper-hashpassword-and-verifypassword?answertab=active#tab-top – Rohit Suthar Feb 25 '15 at 09:56
  • There are couple of things you need to check. 1) In the beforeSave method can you store the raw password first and check in the db to ensure there are no spaces. 2) Your database column size must be more than 60 chars for saving password. 3) Before verify just print both the hash and raw password and see if they are what you expect them to be? – gpr Feb 26 '15 at 06:40
  • i found the problem . length of field in db was 50, so it could not save passwords completely. thank you. – demortz Feb 26 '15 at 06:49

0 Answers0