【发布时间】:2017-01-30 03:10:53
【问题描述】:
我正在开发 CakePHP 3.3。我希望用户使用 电子邮件 或 手机号码 以及 密码 登录。
我有带有email, mobile, password, etc 字段的用户表。
根据 CakePHP doc,我正在使用自定义查找器 auth 登录。
AppController.php中的Auth组件
$this->loadComponent('Auth', [
'loginRedirect' => [
'controller' => 'Dashboard',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Pages',
'action' => 'home'
],
'authenticate' => [
'Form' => [
'finder' => 'auth'
]
]
]);
和findAuth()UsersTable.php中的操作
public function findAuth(Query $query, array $options)
{
$query
->select(['id', 'email', 'mobile', 'password'])
->where(['Users.email' => $options['login']])
->orWhere(['Users.mobile' => $options['login']]);
return $query;
}
UsersController.php 中的 login() 操作
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->registerError(__('Invalid Credentials, try again'), ['key' => 'registration']);
}
}
login.ctp 视图包含
<?php
echo $this->Form->create();
echo $this->Form->input('login');
echo $this->Form->input('password');
echo $this->Form->submit();
echo $this->Form->end();
?>
但这不起作用并打印 Invalid Credentials, try again
更新 2
在users 表中添加了一个空白列username。
login.ctp
<?php
echo $this->Form->create();
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->submit();
echo $this->Form->end();
?>
AppController.php:authComponent
同上
findAuth()
public function findAuth(Query $query, array $options)
{
$query
->orWhere(['Users.email' => $options['username']])
->orWhere(['Users.mobile' => $options['username']]);
return $query;
}
现在它正在工作。但是,即使在应用程序中不需要,为什么还要强制使用username 列。
【问题讨论】:
-
我用不同的方法进行了测试,但在这里也没有工作。也许 cakephp 不支持某些版本,文档还不够。
标签: authentication cakephp cakephp-3.3