简体   繁体   English

使用电子邮件字段代替用户名字段进行身份验证CakePHP 3

[英]Using email field instead of username field for authentiaction CakePHP 3

I was trying to implement login functionality for a web application. 我试图为Web应用程序实现登录功能。 I found that CakePHP not allowing authentication if I using field other than 'username'. 我发现CakePHP不允许使用“用户名”以外的其他字段进行身份验证。 Can you please explain how can I use email field instead of username field for authentication. 您能解释一下如何使用电子邮件字段代替用户名字段进行身份验证吗?

I am sharing Users table and authentication code below:: 我在下面共享用户表和身份验证代码::

Users Table 用户表

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;

Authentication Code 验证码

$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')
        ]);

Change: 更改:

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

To: 至:

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

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

相关问题 在CakePHP的OAuth2插件中无法使用“电子邮件”字段代替“用户名” - Cannot use 'email' field instead of 'username' in OAuth2 Plugin for CakePHP cakephp 2使用用户名和另一个字段登录? - cakephp 2 login with username and another field? Cakephp 2应用程序坚持尝试使用用户名而不是电子邮件进行身份验证 - Cakephp 2 app insists on trying to authenticate with username, instead of email 使用您的用户名或 email 填写登录字段 - Fill in the login field with your username OR email 将yii默认用户名字段更改为电子邮件字段以进行登录 - Changing yii default username field to email field for login 使用Symfony2 / Symfony3中的FOSUserBundle删除/用电子邮件替换用户名字段 - Remove / Replace the username field with email using FOSUserBundle in Symfony2 / Symfony3 CakePHP使用身份验证组件使用用户名和电子邮件登录 - CakePHP Login with both username and email using Auth Component 使用电子邮件而不是用户名登录 - Login with email instead of username Laravel 问题:如何将email字段更改为用户名? - Laravel question: How can I change the email field to username? Laravel 5.6将电子邮件字段名称更改为用户名以忘记密码 - Laravel 5.6 Change email field name as username for forgot password
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM