Can';t使用CakePHP'登录;s身份验证登录
我知道这是一个吃多了的问题,但我无法在所有答案中找到答案。也许你能帮助我。我正在尝试登录一个用户,并且我得到了“用户名/密码无效”错误和正确的数据。这是我第一次使用cakePHP 遵守守则 型号:Can';t使用CakePHP'登录;s身份验证登录,php,cakephp,login,Php,Cakephp,Login,我知道这是一个吃多了的问题,但我无法在所有答案中找到答案。也许你能帮助我。我正在尝试登录一个用户,并且我得到了“用户名/密码无效”错误和正确的数据。这是我第一次使用cakePHP 遵守守则 型号: App::uses('AppModel','Model'); Class User extends AppModel { public $useTable = 'Users'; public $hasMany = array( 'Costumer' => a
App::uses('AppModel','Model');
Class User extends AppModel {
public $useTable = 'Users';
public $hasMany = array(
'Costumer' => array(
'className' => 'Costumer',
'foreignKey' => 'users_id',
'order' => 'Costumer.name ASC'
)
);
//Suppressed the validation code, don't think it's important here
public function beforeSave($options = array()){
if (!empty($this->data['User']['pwd'])) {
$this->data['User']['passwd'] = Security::hash($this->data['User']['pwd']);
}
}
}
控制器:
App::uses('AppController', 'Controller');
class UsersController extends AppController{
public $helpers = array('Html', 'Form');
public $name = 'Users';
public $components = array('Auth','Session');
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('add');
}
public function login(){
//Tests
$userEmail = $this->User->findByEmail($this->request->data['User']['email']);
$userPass = $this->User->findByPasswd(Security::hash($this->request->data['User']['passwd']));
die(var_dump($userEmail, $userPass));
if ($this->request->is('post')) {
$this->request->data['User']['passwd'] = Security::hash($this->request->data['User']['passwd']);
if ($this->Auth->login()){
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('E-mail e/ou usuário incorretos, tente novamente.'));
}
}
}
视图:
}
疯狂的是,两条用户控制器的线路
$userEmail = $this->User->findByEmail($this->request->data['User']['email']);
及
返回我试图登录的用户,这样就不会出现数据错误
伙计们!我错过了什么
谢谢
编辑
因为我还没有找到任何方法以“优雅”的方式完成这项工作,所以我编写了一个虚拟解决方案。它根据数据库手动检查请求->数据值,并手动登录用户。这是一个暂时的解决办法,我稍后再谈
public function login(){
if ($this->request->is('post')) {
$user = $this->User->findByEmail($this->request->data['User']['email']);
if (!empty($user) && ($user['User']['passwd'] == Security::hash($this->request->data['User']['passwd']))){
$this->Auth->login($this->request->data);
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('E-mail e/ou usuário incorretos, tente novamente.'));
}
}
}
我不熟悉Auth组件的passwordHasher属性,也不熟悉Blowfish 您应该使用Cake的内置密码哈希器 用户模型
public function beforeSave() {
if (isset($this->data['User']['passwd'])) {
$this->data['User']['passwd'] = AuthComponent::password($this->data['User']['passwd']);
}
return true;
}
AppController
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'pages', 'action' => 'display', 'homemk'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'homemk'),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email', 'password' => 'passwd'),
)
),
'authError' => 'Para visualizar esta página, você precisa estar logado.'
)
);
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()){
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('E-mail e/ou usuário incorretos, tente novamente.'));
} // end if cannot log in
} // end if no form submitted
} // end login
用户控制器
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'pages', 'action' => 'display', 'homemk'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'homemk'),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email', 'password' => 'passwd'),
)
),
'authError' => 'Para visualizar esta página, você precisa estar logado.'
)
);
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()){
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('E-mail e/ou usuário incorretos, tente novamente.'));
} // end if cannot log in
} // end if no form submitted
} // end login
这是一个很好的提示,bowlerae,但仍然显示出相同的错误。真倒霉这很奇怪,因为我遵循了2.x食谱和示例。快速转到您的数据库,将您的用户密码之一更改为“23235BBC17A56627A5BECF7A74CBFEA0B49FA5A”。这是单词“密码”的蛋糕密码哈希。然后尝试使用他们的电子邮件登录该用户并键入“密码”。它可能不起作用,因为密码是使用旧哈希保存在您的表中的。我认为这不是问题所在,因为我从users表中删除了所有用户,并使用AuthComponent::password方法创建了一个新的用户。但我会试试的。不幸的是没有。正如您所建议的,我尝试直接在数据库中更改密码。同样疯狂的是,它不会让用户登录,但是测试查询“findByEmail”和“findByPasswd”返回的结果是正确的。我想知道这是否与使用电子邮件登录用户有关。我只使用过“用户名”,但我读过关于使用“电子邮件”的文档,它看起来很简单,而且你的代码看起来很正确。
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()){
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('E-mail e/ou usuário incorretos, tente novamente.'));
} // end if cannot log in
} // end if no form submitted
} // end login