# Login does not work

**URL:** <https://discourse.cakephp.org/t/login-does-not-work/7850>\
**Category:** Need Help\
**Created:** [May 22, 2020, 10:18am UTC](https://discourse.cakephp.org/t/login-does-not-work/7850 "2020-05-22T10:18:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![shashi](https://avatars.discourse-cdn.com/v4/letter/s/ecb155/32.png) [@shashi](https://discourse.cakephp.org/u/shashi)\
**Post date:** [May 22, 2020, 10:18am UTC](https://discourse.cakephp.org/t/login-does-not-work/7850/1 "2020-05-22T10:18:55Z")

</div>

userscontroller.php  
public function login(){  
if($this-\>request-\>is(‘post’)){  
echo “

```
”;print_r($this->request->is(‘post’)); die;
$user = $this->Auth->identify();
//echo “
```
”;print_r($user); die;
if($user){
$this->Auth->setUser($user);
$this->Flash->success(**(‘Login successful.’));  
return $this-\>redirect([‘Controller’=\>‘Users’, ‘action’=\>‘index’]);  
}  
$this-\>Flash-\>error(**(‘user could not login.try again.’));
}
}

login.ctp

### Login
 \<?= $this-\>form-\>create(); ?\> \<?= $this-\>Form-\>control('email'); ?\> \<?= $this-\>Form-\>control('password', array('type'=\> 'password')); ?\> \<?= $this-\>Form-\>submit('Login',array('class'=\>'button')); ?\> \<?= $this-\>form-\>end(); ?\> 

Appcontroller.php
 public function initialize()
    {
        parent::initialize();

```
    $this->loadComponent('RequestHandler', [
        'enableBeforeRedirect' => false,
    ]);
    $this->loadComponent('Flash');
  $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ]
            ]
        ],
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ]
    ]);
```
```
```
