首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"Auth->identify()“总是返回false - CakePHP 3.5

"Auth->identify()“总是返回false - CakePHP 3.5
EN

Stack Overflow用户
提问于 2018-02-26 10:08:08
回答 2查看 887关注 0票数 0

因为某些原因,我不能登录到我注册的帐户。下面是更多的信息。

函数来自UsersController.php

代码语言:javascript
复制
public function login() {
    if ($this->request->is('post')) {
        $auth = $this->Auth->identify(); // Returns false
        debug($this->request->getData()); // Return email & with unhashed password
        debug($auth);
        if ($auth) {
            $this->Auth->setUser($auth);
            $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Flash->error('E-mail or password is wrong.');
        }
    }
}

public function register() {
    $user = $this->Users->newEntity();
    $this->set('user', $user);

    $this->loadModel('Groups');
    $group = $this->Groups->newEntity();
    $this->set('group', $user);

    if ($this->request->is('post')) {

        // Check if passwords matches
        $pass = $this->request->getData('password');
        $con_pass = $this->request->getData('password_confirm');

        if ($pass !== $con_pass) {
            return $this->Flash->error('Passwords don\'t match');
        }

        // Patch entities
        $group = $this->Groups->patchEntity($group, $this->request->getData());
        $user = $this->Users->patchEntity($user, $this->request->getData());

        // Make group and user
        if (empty($group->errors()) && empty($user->errors())) {
            // Group
            if (!$this->Groups->save($group)) {
                return $this->Flash->error('Something went wrong');
            }

            // User
            $user->group_id = $group->id;
            if ($this->Users->save($user)) {
                $this->Flash->success('Welkom ' . $user->name . '!');
                // return $this->redirect(['action' => 'register']);
            } else {
                return $this->Flash->error('something went wrong2');
            }
        }
    }
}

AppController:中的Auth组件

代码语言:javascript
复制
        $this->loadComponent('Auth', [
        'userModel' => 'Users',
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ]
            ]
        ],
        //'authError' => false,
        'storage' => 'Session'
    ]);

登录表格:

代码语言:javascript
复制
    <?= $this->Form->create('User'); ?>
    <?= $this->Form->email('email', ['placeholder' => 'E-mail', 'maxlength' => '42', 'label' => false]) ?>
    <?= $this->Form->password('password', ['type' => 'password', 'placeholder' => 'Wachtwoord', 'maxlength' => '32', 'label' => false]) ?>
    <?= $this->Form->submit('Login', ['class' => 'button']) ?>
<?= $this->Form->end(); ?>

用户实体:

代码语言:javascript
复制
class User extends Entity {

protected $_accessible = [
    'group_id' => true,
    'name' => true,
    'email' => true,
    'password' => true,
    'profile_img_url' => true,
    'pass_reset_time' => true,
    'creation_date' => true,
    'modified_date' => true
];

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

protected $_hidden = [
    'password'
];

}

用户将使用散列密码正确地保存在数据库中。

当我尝试登录时,$this->Auth->identify();总是返回false。

我想知道的是:

  • 我试着用电子邮件和密码登录。
  • db中的表名是users
  • 更新salt (并创建一个新帐户并使用该帐户登录)
  • 密码列长度为255。
  • 检查八月博士
  • 检查博客教程
  • 在Stack和其他网站上检查了很多与此相关的问题,但是还没有解决我的问题。
  • 用户将被正确地存储。但一旦我尝试登录,它就不会让我登录。
  • 我试图登录没有密码有她的功能和一个未散列的密码,也没有工作。
  • 签入不同的浏览器和已删除的缓存。

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2018-02-26 13:51:32

似乎没有任何明显的错误,除了在_setPassword()方法中缺少一个空检查,这将防止空$password被散列。您应该做一些类似于文档中显示的事情:

代码语言:javascript
复制
if (strlen($password) > 0) {
    return (new DefaultPasswordHasher)->hash($password);
}

请参阅Cookbook > Controllers > Components > Authentication > Hashing Passwords

另外,FormHelper::create()方法也不接受字符串,它只不会因为向后兼容的原因而出错。如果您没有要传递的有效上下文,则根本不传递任何值。

话虽如此,您将不得不自己做更多的调试。首先,使用DefaultPasswordHasher::validate()方法手动验证存储在数据库中的散列密码,以确保正确的值已被散列。

然后,在身份验证代码流中设置一些断点,以确定可能出错的地方,请检查:

  • FormAuthenticate::authenticate()
  • FormAuthenticate::_checkFields()
  • BaseAuthenticate::_findUser()
  • BaseAuthenticate::_query()

是否读取正确的请求数据,是否按预期构建查询条件,是否返回用于密码验证的值,等等.

票数 2
EN

Stack Overflow用户

发布于 2018-02-26 14:55:33

好吧,我浪费了整个上午和下午。

我以为我的密码列长是255,但实际上是32。显然,我检查了错误的列的长度,大约4次。

谢谢帮助@ndm。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48985886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档