简体   繁体   English

用户登录无效,显示错误“用户名或密码错误”

[英]Users login not working, showing error “incorrect username or password”

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. 我有UsersController,我正在尝试使用电子邮件和密码字段登录,我也在使用密码哈希,而且我的密码字段是text数据类型。 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)

Please add password hasher at your Entity Class( scr/Model/Entity/User.php ) 请在您的实体类(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 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 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] User.php [实体]

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

login.ctp 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 如果上述代码对您不起作用,您可以尝试在AppController中添加此代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Joomla登录错误:500时用户名和密码不正确 - Joomla Login error: 500 when the username and password this incorrect 为什么我的登录表单允许用户使用正确的USERNAME但错误的PASSWORD登录? (查看详细信息) - Why my login form allowing users to login with correct USERNAME, but incorrect PASSWORD? (See detail) 登录使用错误的用户名和密码访问该帐户 - login access the account with incorrect username and password Cakephp登录表单中的密码或用户名不正确 - Incorrect password or username in login form in cakephp 登录错误密码和用户名组合 - login error password and username combination Symfony 4:登录不适用于令牌,但适用于用户名和密码 - Symfony 4: Login not working for token, but working for username and password 用户登录脚本:用户可以使用任何用户名和密码登录 - User login script: Users can login with any username and password 电子邮件/用户名组合是不正确的登录系统错误 - Email/Username combination is incorrect error in login system 即使密码或用户名不正确,我的 PHP 登录系统仍然在登录 - My PHP login system still Logging in even if the password or username is incorrect PHP登录页面始终返回错误的用户名和密码 - PHP Login Page Always Returning Incorrect Username and Password
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM