使用email字段而不是username字段进行authentication cakephp3

up9lanfz  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(404)

我试图为一个web应用程序实现登录功能。我发现cakephp不允许身份验证,如果我使用的字段不是'用户名'。你能解释一下我如何使用email字段而不是username字段进行身份验证吗。
我正在共享下面的用户表和身份验证代码:
用户表

CREATE TABLE `users` (
  `id` binary(36) NOT NULL DEFAULT 'NOT NULL\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
  `password` varchar(225) NOT NULL,
  `firstname` varchar(50) NOT NULL,
  `lastname` varchar(50) NOT NULL,
  `email` varchar(45) NOT NULL,
  `phone` varchar(15) NOT NULL DEFAULT '0',
  `dob` date NOT NULL,
  `profile_for` varchar(50) DEFAULT NULL,
  `profile_pic` varchar(255) DEFAULT NULL,
  `cover_photo` varchar(100) DEFAULT NULL,
  `package_id` binary(36) DEFAULT NULL,
  `status` enum('act','dct','hid') NOT NULL DEFAULT 'act',
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `mobile` (`phone`),
  UNIQUE KEY `email_UNIQUE` (`email`),
  KEY `package_id_idx` (`package_id`),
  CONSTRAINT `package_id` FOREIGN KEY (`package_id`) REFERENCES `packages` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

身份验证码

$this->loadComponent('Auth', [
            'loginAction'=> [
                'controller' => 'Users',
                'action' => 'login'
            ],
            'loginRedirect'=>[
                'controller' => 'Users',
                'action' => 'dashboard'
            ],
            'logoutRedirect'=>[
                'controller' => 'Users',
                'action' => 'login'
            ],
            'authError' => 'Did you really think you are allowed to see that?',
            'authenticate' => [
                'Form' => ['email' => 'email', 'password' => 'password']
            ],
            'storage' => 'Session',
            'authorize' => array('Controller')
        ]);
r6hnlfcb

r6hnlfcb1#

更改:

'authenticate' => [
    'Form' => ['email' => 'email', 'password' => 'password']
],

收件人:

'authenticate' => [
    'Form' => ['username' => 'email', 'password' => 'password']
],

相关问题