dsg41888 2018-02-17 08:21
浏览 66

用户登录不工作,显示错误“用户名或密码不正确”

I have UsersController i'm trying to login using email and password fields, i'm also using Password Hashing, also my password field is of text datatype. which is working fine, but i'm unable to login, showing me an error

username or password is incorrect

UsersController.php

public function login()
    {
        if ($this->request->is('post'))
        {
            $user = $this->Auth->identify();
            if ($user)
            {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error('Your username or password is incorrect.');
        }

    }

AppController.php

   public function initialize()
        {
            parent::initialize();
            $this->loadComponent('RequestHandler');
            $this->loadComponent('Flash');
            $this->loadComponent('Auth',[
                'authenticate'=>[
                    'Form'=>[
                        'fields'=>[
                            'username'=>'email',
                            'password'=>'password'
                        ]
                    ]
                ],

                'loginRedirect'=>[
                    'controller'=>'Users',
                    'action'=>'index'
                ],

                'loginAction'=>[
                    'controller'=>'Users',
                    'action'=>'login'
                ]
            ]);
             $this->Auth->allow(['add']);
}

login.ctp

<h1>Login</h1>

<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->control('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>

UsersTable

id char(100)
name varchar(100)
email varchar(100)
password (text)
  • 写回答

1条回答 默认 最新

  • dongzhuo1930 2018-02-18 06:55
    关注

    Please add password hasher at your Entity Class( scr/Model/Entity/User.php )

        use Cake\Auth\DefaultPasswordHasher;    
        protected function _setPassword($password)
            {
                if (strlen($password) > 0) {
                  return (new DefaultPasswordHasher)->hash($password);
                }
            }
    

    After adding this, make a new user then try to login from newly created user.

    I did this which is working here

    UsersController.php

    public function login()
    {
        if ($this->request->is('post'))
        {
            $user = $this->Auth->identify();
            if ($user)
            {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error('Your username or password is incorrect.');
        }
    
    }
    
    public function logout()
    {
        return $this->redirect($this->Auth->logout());
    }
    

    AppController.php

    $this->loadComponent('Auth',[
                'authenticate'=>[
                    'Form'=>[
                        'fields'=>[
                            'username'=>'email',
                            'password'=>'password'
                        ]                        
                    ]
                ],
    
                'loginRedirect'=>[
                    'controller'=>'Users',
                    'action'=>'index'
                ],
    
                'loginAction'=>[
                    'controller'=>'Users',
                    'action'=>'login'
                ]
            ]);
             $this->Auth->allow(['add']);
    

    User.php [Entity]

    protected function _setPassword($password)
    {
        if (strlen($password) > 0) {
          return (new DefaultPasswordHasher)->hash($password);
        }
    }
    

    login.ctp

    <h1>Login</h1>
    <?= $this->Form->create() ?>
    <?= $this->Form->control('email') ?>
    <?= $this->Form->control('password') ?>
    <?= $this->Form->button('Login') ?>
    <?= $this->Form->end() ?>
    

    If do not work the above code for you, you can try adding this at your AppController

    'fields'=>[ 'username'=>'email', 'password'=>'password' ], 
    'userModel' => 'Users'
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看