php - 当电子邮件和密码位于单独的表 cakephp 中时验证用户

标签 php mysql cakephp

表单接收电子邮件和密码

<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->control('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>

email作为ID存储在Users中,Password在password表中 地址是电子邮件表中存储实际电子邮件地址的属性 密码就是pw存放的地方

认证组件接收地址——

$this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'fields' => [
                    //here we define what is compared to be authenticated
                    'username' => 'address',    
                    'password' => 'password'
                ]
            ]...

登录功能和正常一样:

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

        //PUT IN STUFF HERE
        $user = $this->Auth->identify();


        if ($user) {
            $user->last_login = Time::now();//dont put this above uif statement, will automatically create a default object
            $this->Auth->setUser($user);
            $this->Flash->success('You have been successfully logged in.');
            $this->log("Login success", 'debug');
          //redirect after login
            return $this->redirect($this->Auth->redirectUrl('/users/index'));
        }
        $this->Flash->error('Your username or password is incorrect.');
        $this->log("Login FAILURE", 'debug');
    }
}`

我怎么看,我们要么比较电子邮件 ID,要么让表单直接查看关联类的“地址”属性。如何将身份验证指向另一个表中的属性

谢谢

最佳答案

您必须创建 Custom Authentication Objects为此

On load component

$this->loadComponent('Auth', [
        'authenticate' => [
            'CustomForm' => [
                'fields' => [
                    'username' => 'address',// Field in your emails table
                    'password' => 'password',// Field in your users table
                    'myAssoc'=>'Users'// Custom Filed to get association
                ],
                'userModel' => 'Emails'
            ]...

Create a file CustomFormAuthenticate.php in /src/Auth/ folder

<?php

namespace App\Auth;

use Cake\Auth\FormAuthenticate;
use Cake\Utility\Inflector;

class CustomFormAuthenticate extends FormAuthenticate
{
    public function _findUser($username, $password = null)
    {
        $result = $this->_query($username);
        $myAssoc = false;
        if (!empty($this->_config['fields']['myAssoc'])) {
            $myAssoc = $this->_config['fields']['myAssoc'];
            $result->contain([$myAssoc]);
        }

        $result = $result->first();

        if (empty($result)) {
            return false;
        }

        if ($password !== null) {
            $hasher = $this->passwordHasher();
            if($myAssoc !== false){
                $hashedPassword = $result->{Inflector::underscore(Inflector::singularize($myAssoc))}[$this->_config['fields']['password']];
            } else {
                $hashedPassword = $result->get($this->_config['fields']['password']);
            }

            if (!$hasher->check($password, $hashedPassword)) {
                return false;
            }

            $this->_needsPasswordRehash = $hasher->needsRehash($hashedPassword);
            $result->unsetProperty($this->_config['fields']['password']);
        }
        debug($result);
        return $result->toArray();
    }

}

Make sure you have association of model Users with Email in your EmailTable.php

$this->hasOne('Users', [
    'foreignKey' => 'email_id'
]);

In your login page

<?= $this->Form->create() ?>
<?= $this->Form->control('address') ?> // Field in your email table
<?= $this->Form->control('password') ?>// Field in your users table
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>

我已经测试过它并且它对我有用。

关于php - 当电子邮件和密码位于单独的表 cakephp 中时验证用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43750804/

相关文章:

PHP 读取 cookie 文件

mysql - 无法在 WSL 上安装 mysql-server

php - Laravel,将加密的 secret 存储在 MySQL 索引列中

MySQL如何在一个没有公共(public)键的情况下连接3个表

php - CakePHP 输出一个选择输入,用于从连接表中选择一个值

php - 查询。如果列 = 特定值,则返回所有

php - 使用 PHP Mysql 从表单中插入日期

php - 无法在 phpmyadmin 中打开某些表

javascript - Cakephp 中的可重复子表单

php - CakePHP 电子邮件插件 - 使用自定义服务器