webdbapps Posted July 31, 2012 Share Posted July 31, 2012 Hi, I'm trying to implement the authentication techniques documented in Larry's awesome blog post. Here's the issue: Correct login credentials are entered into the AJAX login form on the home page. Upon pressing enter, a new Login form opens and no indication is present that user is authenticated. If that login form is then populated, again no further indication is present that we have authenticated. UserController.php actionLogin: public function actionLogin() { $model= new Users(); // Yii::app()->sessiion[username]= Yii::app()->model($username); // Yii:app()->session[password] = Yii::app()->model($password); // if it is ajax validation request if(isset($_POST['ajax'])) { echo CActiveForm::validate($model); Yii::app()->end(); } // print_r($_POST['Users']); // collect user input data $form = new LoginForm; // if(isset($_POST['Users'])) // { // $model->attributes=$_POST['Users']; // validate user input and redirect to the previous page if valid // if($model->login()) // $this->redirect(Yii::app()->user->getReturnUrl('index')); // } // display the login form // $this->render('login',array('model'=>$model)); if(isset($_POST['LoginForm'])){ $form->attributes=$_POST['LoginForm']; if($form->validate() && $form->login()) $this->redirect(Yii::app()->user->returnUrl); } $this->render('login',array('model'=>$model)); } LoginForm model authenticate: public function authenticate($attribute,$params) { if(!$this->hasErrors()) { // $this->_identity=new UserIdentity($this->username,$this->password); // if(!$this->_identity->authenticate()) // $this->addError('password','Incorrect username or password.'); $identity = new UserIdentity($this->username, $this->password); $identity->authenticate(); switch($identity->errorCode) { case UserIdentity::ERROR_NONE: $duration=$this->rememberMe ? 3600*24*30 : 0; //30 days Yii::app()->user->login($identity,$duration); break; case UserIdentity::ERROR_USERNAME_INVALID: $this->addError('username', 'Username is incorrect.'); break; default; // UserIdentity:ERROR_USERNAME_INVALID $this->addError('password','Password is incorrect.'); break; } } } /views/users/login.php <div class="row"> <?php echo $form->labelEx($model,'username'); ?> <?php echo $form->textField($model,'username'); ?> <?php echo $form->error($model,'username'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'password'); ?> <?php echo $form->passwordField($model,'password'); ?> <?php echo $form->error($model,'password'); ?> </div> <div class="row rememberMe"> <?php echo $form->checkBox($model,'rememberMe'); ?> <?php echo $form->label($model,'rememberMe'); ?> <?php echo $form->error($model,'rememberMe'); ?> </div> /protected/components/UserIdentity.php public function authenticate() { // $username = $this->username; // $password = $this->password; $user = Users::model()->findbyAttributes(array($username=>$this->username)); if($user === NULL){ $this->errorCode=self::ERROR_UNKNOWN_IDENTITY; }else if ($user->password !== md5($this->password)){ // $this->username = $user->username; // sess('SESS_USER_INFO', $user->attributes); // $this->errorCode=self::ERROR_NONE; //invalid password $this->errorCode=self::ERROR_PASSWORD_INVALID; } else { $this->errorCode=self::ERROR_NONE; } return !$this->errorCode; } I feel like I'm so close to fixing this up, but yet cannot identify the root cause(s). Can anyone provide suggested resolutions? Link to comment Share on other sites More sharing options...
Larry Posted August 11, 2012 Share Posted August 11, 2012 Sorry for the delayed reply. I was on vacation. Do you still need help with this? Link to comment Share on other sites More sharing options...
webdbapps Posted August 22, 2012 Author Share Posted August 22, 2012 I was able to work though it. Thanks. Link to comment Share on other sites More sharing options...
Larry Posted August 22, 2012 Share Posted August 22, 2012 Great. Sorry I wasn't able to help. For the benefit of others, could you post the solution? Link to comment Share on other sites More sharing options...
Recommended Posts