Cakephp 3 - 身份验证登录 Return 错误
Cakephp 3 - Auth Login Return False
这段代码有什么问题?
Controller 和 Identify only return false!
我的数据库列是 senha(密码)和电子邮件。而且我无法登录。
我正在使用哈希,密码为 255 个字符,没问题。
但它不起作用!
ContaController.php
public function initialize() {
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => ['email' => 'email', 'senha' => 'senha'],
'userModel' => 'Conta',
'finder' => 'auth',
]
],
'authorize' => ['Controller'],
'loginAction' => [
'controller' => 'Conta',
'action' => 'index',
],
'loginRedirect' => [
'controller' => 'Conta',
'action' => 'minha-agenda'
],
'logoutRedirect' => [
'controller' => 'Conta',
'action' => 'index',
],
'storage' => 'Memory'
]);
$this->Auth->allow(['index']);
}
public function index() {
if ($this->request->is('ajax') || $this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
// return $this->redirect($this->Auth->redirectUrl());
echo 'success';
} else {
var_dump($user);
echo 'incorrect';
}
}
}
ContaTable.php
public function initialize(array $config) {
parent::initialize($config);
$this->setTable('alunos');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsToMany('Alunos', [
'foreignKey' => 'interesses_id',
'targetForeignKey' => 'alunos_id',
'joinTable' => 'alunos_interesses'
]);
}
public function validationDefault(Validator $validator) {
$validator
->notEmpty('email', 'A username is required')
->notEmpty('senha', 'A password is required');
return $validator;
}
public function findAuth(\Cake\ORM\Query $query, array $options) {
$query
->select(['id', 'email', 'senha'])
->where(['Conta.email' => $options['email']])
->andWhere(['Conta.senha' => $options['senha']]);
return $query;
}
我需要帮助解决这个问题。
数据库中的列不同,所以我不打算将其用作默认值。
再往前会变成ajax,但现在是这样,因为我无法解决!
将'fields' => ['email' => 'email', 'senha' => 'senha']
更改为'fields' => ['username' => 'email', 'password' => 'senha']
使用此配置,您不需要自定义查找器,除非您需要 select 列。通过使用默认查找器,您可以随时取消设置属性。
确保您的登录表单控件和 table 列被命名为 email
和 senha
然后,我想知道你是否应该在 AppController 中加载 authcomponent。
这段代码有什么问题? Controller 和 Identify only return false! 我的数据库列是 senha(密码)和电子邮件。而且我无法登录。 我正在使用哈希,密码为 255 个字符,没问题。 但它不起作用!
ContaController.php
public function initialize() {
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => ['email' => 'email', 'senha' => 'senha'],
'userModel' => 'Conta',
'finder' => 'auth',
]
],
'authorize' => ['Controller'],
'loginAction' => [
'controller' => 'Conta',
'action' => 'index',
],
'loginRedirect' => [
'controller' => 'Conta',
'action' => 'minha-agenda'
],
'logoutRedirect' => [
'controller' => 'Conta',
'action' => 'index',
],
'storage' => 'Memory'
]);
$this->Auth->allow(['index']);
}
public function index() {
if ($this->request->is('ajax') || $this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
// return $this->redirect($this->Auth->redirectUrl());
echo 'success';
} else {
var_dump($user);
echo 'incorrect';
}
}
}
ContaTable.php
public function initialize(array $config) {
parent::initialize($config);
$this->setTable('alunos');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsToMany('Alunos', [
'foreignKey' => 'interesses_id',
'targetForeignKey' => 'alunos_id',
'joinTable' => 'alunos_interesses'
]);
}
public function validationDefault(Validator $validator) {
$validator
->notEmpty('email', 'A username is required')
->notEmpty('senha', 'A password is required');
return $validator;
}
public function findAuth(\Cake\ORM\Query $query, array $options) {
$query
->select(['id', 'email', 'senha'])
->where(['Conta.email' => $options['email']])
->andWhere(['Conta.senha' => $options['senha']]);
return $query;
}
我需要帮助解决这个问题。 数据库中的列不同,所以我不打算将其用作默认值。 再往前会变成ajax,但现在是这样,因为我无法解决!
将'fields' => ['email' => 'email', 'senha' => 'senha']
更改为'fields' => ['username' => 'email', 'password' => 'senha']
使用此配置,您不需要自定义查找器,除非您需要 select 列。通过使用默认查找器,您可以随时取消设置属性。
确保您的登录表单控件和 table 列被命名为 email
和 senha
然后,我想知道你是否应该在 AppController 中加载 authcomponent。