I would like a user to be able to login to my app at site/index.php
with an ajax request when the user provides a correct username and password the first time. If username or password is wrong the first time, it shows a captcha then if all inputs are correct don't show captcha error.
This my code:
loginForm
<?php
/**
* LoginForm class.
* LoginForm is the data structure for keeping
* user login form data. It is used by the 'login' action of 'SiteController'.
*/
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;
public $verifyCode;
private $_identity;
/**
* Declares the validation rules.
* The rules state that username and password are required,
* and password needs to be authenticated.
*/
public function rules()
{
return array(
array('username, password', 'required'),
array('rememberMe', 'boolean'),
array('password', 'authenticate'),
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(), 'on' => 'captchaNeed'),
);
}
/**
* Authenticates the password.
* This is the 'authenticate' validator as declared in rules().
*/
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.');
}
}
}
/**
* Logs in the user using the given username and password in the model.
* @return boolean whether login is successful
*/
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;
}
}
site/index
/**
* This is the default 'index' action that is invoked
* when ad action is not explicitly requested by users.
*/
public function actionIndex()
{
// set default layout
$this->layout = 'main';
$loginModel = new LoginForm;
if (Yii::app()->request->isAjaxRequest) {
if (isset(Yii::app()->session['captchaNeed'])) {
LoginModel->scenario = 'captchaNeed';
}
if (isset($_POST['LoginForm'])) {
$loginModel->attributes = $_POST['LoginForm'];
if ($loginModel->validate() && $loginModel->login()) {
echo '<script>window.location= "' . Yii::app()->createUrl('site/dashboard') . '"</script>';
unset(Yii::app()->session['captchaNeed']);
Yii::app()->end();
} else {
Yii::app()->session['captchaNeed'] = true;
$loginModel->scenario = 'captchaNeed';
}
$this->renderPartial('_login', array('model' => $loginModel));
}
}
if (!Yii::app()->request->isAjaxRequest) {
$this->render('index', array('loginModel' => $loginModel));
}
}
siteController actions
public function actions()
{
return array(
'captcha' => array(
'class' => 'CCaptchaAction',
'minLenght' => 4,
'maxLenght' => 4,
'testLimit' => 1,
'backColor' => 0x7D287E,
'foreColor' => 0xFFFFFF,
'height' => 24,
'width' => 100,
'offset' => 1,
),
);
}
view\site\index
<section class="col-xs-8" id="index-login">
<?php if (Yii::app()->user->isGuest) : ?>
<div id="login">
<?php $this->renderPartial('_login', array('model' => $loginModel)) ;?>
</div>
<?php echo CHtml::ajaxSubmitButton(
'Login',
Yii::app()->createUrl('site/index'),
array(
'type' => 'post',
'update' => '#login',
'data' => 'js:$("#login-form").serialize();
),
array(
'id'=>uniqid(),
'class'=>'btn btn-danger',
)
); ?>
<?php endif; ?>
</section>
view\site_login
<?php
/* @var $this SiteController */
/* @var $model LoginForm */
/* @var $form CActiveForm */
$this->pageTitle =Yii::app()->name . ' - Login';
$this->breadcrumbs = array('Login',);
?>
<h1>Login</h1>
<p>Please fill out the following form with your login credentials:</p>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
)
);?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<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'); ?>
<p class="hint">Hint: You may login with <kbd>demo</kbd>/<kbd>demo</kbd> or <kbd>admin</kbd>/<kbd>admin</kbd>.
</p>
</div>
<?php if (CCaptcha::checkRequirements() && $model->scenario == 'captchaNeed'): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
</div>
<div class="hint">Please enter the letters as they are shown in the image above.
<br/>Letters are not case-sensitive.
</div>
<?php echo $form->error($model,'verifyCode'); ?>
</div>
<?php endif; ?>
<div class="row rememberMe">
<?php echo $form->checkBox($model,'rememberMe'); ?>
<?php echo $form->label($model,'rememberMe'); ?>
<?php echo $form->error($model,'rememberMe'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Login'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->