Jump to content
Larry Ullman's Book Forums

Login Returns User To The Login Form Instead Of Home Page


Recommended Posts

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 post
Share on other sites
  • 2 weeks later...
  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...