gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:03:22 25 4
gpt4 key购买 nike

表单接收电子邮件和密码

<?= $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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com