CakePHP身份验证登录操作

时间:2019-05-15 12:02:44

标签: php authentication cakephp cakephp-3.x

我正在尝试从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

Users表结构

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

1 个答案:

答案 0 :(得分:0)

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