首页 > 解决方案 > CakePHP Authentication 登录动作

问题描述

我正在尝试从 CakepHP 设置新​​的身份验证方法。登录时,我在调试中收到缺少凭据的消息。在请求 POST 数据中,提交了电子邮件和密码字段。我错过了什么吗?

    /**
     * Login method
     */
    public function login()
    {
        $result = $this->Authentication->getResult();

        // regardless of POST or GET, redirect if user is logged in
        if ($result->isValid()) {
            $redirect = $this->request->getQuery('redirect', ['controller' => 'Pages', 'action' => 'display', 'home']);
            return $this->redirect($redirect);
        }

        // display error if user submitted and authentication failed
        if ($this->request->is(['post']) && !$result->isValid()) {
            $this->Flash->error('Invalid username or password');
        }
    }


public function getAuthenticationService(ServerRequestInterface $request, ResponseInterface $response)
    {
        $service = new AuthenticationService();

        $fields = [
            'username' => 'email',
            'password' => 'password'
        ];

        // Load identifiers
        $service->loadIdentifier('Authentication.Password', compact('fields'));

        // Load the authenticators, you want session first
        $service->loadAuthenticator('Authentication.Session');
        $service->loadAuthenticator('Authentication.Form', [
            'fields' => $fields,
            'loginUrl' => '/users/login'
        ]);

        return $service;
    }

编辑:使用用户名/帖子字段编辑表单验证器选项后,我得到以下信息

错误:FAILURE_IDENTITY_NOT_FOUND

SQL 调试说记录在用户表中找到。

SELECT 
  Users.id AS `Users__id`, 
  Users.email AS `Users__email`, 
  Users.password AS `Users__password`, 
  Users.role AS `Users__role`, 
  Users.created AS `Users__created`, 
  Users.modified AS `Users__modified` 
FROM 
  users Users 
WHERE 
  Users.email = 'my@my.com' 
LIMIT 
  1

用户表结构

id Primary key  int(11)
email   varchar(255)    utf8mb4_general_ci
password    varchar(255)    utf8mb4_general_ci
role    varchar(255)
created datetime
modified    datetime

标签: phpauthenticationcakephpcakephp-3.x

解决方案


已修复,密码为纯文本。添加了一个具有默认密码哈希的新用户。


推荐阅读