Difference between #2 and #3 of
Login Form With email or Whatever

Changes

Title unchanged

Login Form With email or Whatever

Category unchanged

How-tos

Yii version unchanged

Tags unchanged

LoginForm, UserIdentity

Content changed

In login form model
 
 
```php
Here is my solution to login with email or anything else
 
This update works with database mysql
 
**You must generate the user model with GII**
 
 
My login form look like this
 
 
 
 
```php 
<?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',
 
'enableClientValidation'=>true,
 
'clientOptions'=>array(
 
'validateOnSubmit'=>true,
 
),
 
)); ?>
 
 
<p class="note">Fields with <span class="required">*</span> are required.</p>
 
 
<div class="row">
 
<?php echo $form->labelEx($model,'email'); ?>
 
<?php echo $form->textField($model,'email'); ?>
 
<?php echo $form->error($model,'email'); ?>
 
</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>
 
 
<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 -->
 
 
```
 
In SiteController.php no more changes
 
 
```php 
/**
 
 * Displays the login page
 
 */
 
public function actionLogin()
 
{
 
        if (!defined('CRYPT_BLOWFISH')||!CRYPT_BLOWFISH)
 
            throw new CHttpException(500,"This application requires that PHP was compiled with Blowfish support for crypt().");
 
 
        $model=new LoginForm;
 
 
        // if it is ajax validation request
 
        if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
 
        {
 
            echo CActiveForm::validate($model);
 
            Yii::app()->end();
 
        }
 
 
        // collect user input data
 
        if(isset($_POST['LoginForm']))
 
        {
 
            $model->attributes=$_POST['LoginForm'];
 
            // validate user input and redirect to the previous page if valid
 
            if($model->validate() && $model->login())
 
                $this->redirect(Yii::app()->user->returnUrl);
 
        }
 
        // display the login form
 
        $this->render('login',array('model'=>$model));
 
}
 
 
```
 
The LoginForm in models directory
 
 
 
 
```php 
<?php
 

/**
* LoginForm class.
[...]
class LoginForm extends CFormModel
{
public $
password;
 
public $rememberMe;
 
        public $email; // Email
username;
 
    public $email;
 
public $password;
 
public $rememberMe;


private $_identity;
[...]
// username and password are required
array('email, password', 'required'),
            array('email', 'email'),
 
// rememberMe needs to be a boolean array('rememberMe', 'boolean'),                         array('email', 'email'), 
 
// password needs to be authenticated
array('password', 'authenticate'),
);
[...]
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->
email,username, $this->password, $this->email);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect username or password.');
[...]
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->
email,username, $this->password, $this->email);
$this->_identity->authenticate();
}
[...]
```

 
And then extends CUserIdentity
 
finally the UserIdentity in components directory ```php <?php
 
 
/**
* UserIdentity represents the data needed to identity a user.
* It contains the authentication method that checks if the provided
[...]
*/
public $email;
    const ERROR_EMAIL_INVALID=1;
 
 
/** * @pvaram string $email
 
     * @param string $password
 
     */
 int id
 
     */
 
    private $_id;
 
 
    const ERROR_EMAIL_INVALID=1;
 
public function __construct($emailusername, $password, $email) { parent::__construct($emailusername, $password); $this->email=$email; }
 
/** * Authenticates a user. * The example implementation makes sure if the usernameemail and password
* are both 'demo'.
* In practical applications, this should be changed to authenticate
[...]
*/
public function authenticate()
    {
 
$users=array(
 
// username => password
 
'demo@demo.fr'=>'demo',
 
'admin@admin.fr'=>'admin'
 
);
 
if(!isset($users[$this->email])
{
 
        $model=User::model()->findByAttributes(array('email'=>$this->email));
 
if($model===null
) $this->errorCode=self::ERROR_EMAIL_INVALID; elseif($users[$this->email]!==$this!crypt($this->password, $model->password)) $this->errorCode=self::ERROR_PASSWORD_INVALID; else             $this->_id=$model->id;
 
            $this->username=$model->username;
 
$this->errorCode=self::ERROR_NONE; return !$this->errorCode; } /** * @return mixed
 
     */
 
    public function getPassword()
 
    {
 
        return $this->password;
 
    }
 
 
    /**
 
     * @return mixe
integer the ID of the user record */ public function getEmailId() { return $this->email_id; } } ```
 
1 2
2 followers
Viewed: 31 832 times
Version: 1.1
Category: How-tos
Written by: nabil abdouali
Last updated by: nabil abdouali
Created on: Apr 19, 2014
Last updated: 10 years ago
Update Article

Revisions

View all history