title: How works login function in cake php?
body: <p>Login functionality is not working in cake php</p>
  
  <p>I generate UsersController.php</p>
  
  <pre><code>     public function login() {
          if ($this-&gt;Auth-&gt;login()) {
              $this-&gt;redirect($this-&gt;Auth-&gt;redirect());
          } else {
              $this-&gt;Session-&gt;setFlash(__('Invalid username or password, try again'));
          }
       }
  
       public function add() {
           if ($this-&gt;request-&gt;is('post')) {
          $this-&gt;User-&gt;create();
          $this-&gt;request-&gt;data['User']['password'] = md5($this-&gt;request-&gt;data['User']['password']);
          if ($this-&gt;User-&gt;save($this-&gt;request-&gt;data)) {
              $this-&gt;Session-&gt;setFlash(__('The user has been saved'));
              $this-&gt;redirect(array('action' =&gt; 'index'));
          } else {
              $this-&gt;Session-&gt;setFlash(__('The user could not be saved. Please, try again.'));
          }
       }
     }
  </code></pre>
  
  <p>My model file Users.php</p>
  
  <pre><code>            &lt;?php 
              App::uses('AuthComponent', 'Controller/Component');
  
              class User extends AppModel {
                  public $validate = array(
                      'username' =&gt; array(
                      'required' =&gt; array(
                          'rule' =&gt; array('notEmpty'),
                          'message' =&gt; 'A username is required'
                        )
                            ),
                           'password' =&gt; array(
                           'required' =&gt; array(
                              'rule' =&gt; array('notEmpty'),
                           'message' =&gt; 'A password is required'
                        )
                         ),
                       'role' =&gt; array(
                        'valid' =&gt; array(
                            'rule' =&gt; array('inList', array('admin', 'author')),
                                    'message' =&gt; 'Please enter a valid role',
                            'allowEmpty' =&gt; false
                         )
                        )
                    );
  
                     public function beforeSave($options = array()) 
                     {
                          if (isset($this-&gt;data[$this-&gt;alias]['password'])) 
                          {
                           $this-&gt;data[$this-&gt;alias]['password'] = AuthComponent::password($this-&gt;data[$this-&gt;alias]['password']);
                      }
  
                          return true;
                     }
                 }
                 ?&gt;
  </code></pre>
  
  <p>My view file is Uesrs/login.ctp</p>
  
  <pre><code>           &lt;div class="users form"&gt;
  
                 &lt;?php echo $this-&gt;Session-&gt;flash('auth'); ?&gt;
                 &lt;?php echo $this-&gt;Form-&gt;create('User'); ?&gt;
  
                &lt;fieldset&gt;
  
                    &lt;legend&gt;&lt;?php echo __('Please enter your username and password'); ?&gt;&lt;/legend&gt;
                    &lt;?php echo $this-&gt;Form-&gt;input('username');
                       echo $this-&gt;Form-&gt;input('password');
                    ?&gt;
  
                 &lt;/fieldset&gt;
  
                &lt;?php echo $this-&gt;Form-&gt;end(__('Login')); ?&gt;
  
             &lt;/div&gt;
  </code></pre>
  
  <p>When i run this code it tells "Invalid username or password, try again" .</p>
  
  <p>But I enter right user name and password.</p>
  
  <p>Why this issue is coming?</p>
  
  <p>Please help me. I new for Cake Php.</p>