不能得到验证登录使用CakePHP 2.0工作 [英] Can't get Auth login to work with CakePHP 2.0
问题描述
我试图让一个简单的登录表单使用CakePHP 2.0 ... ...只是验证,没有ACL现在的工作。
I'm trying to get a simple login form to work using CakePHP 2.0... just Auth, no ACLs for now.
我能看到的形式,并输入电子邮件地址和密码,因为它们是在数据库中,但我只是得到返回的形式和显示闪光的错误消息。这里是我的code:
I'm able to see the form and enter the email and password as they are in the database, but I just get returned to the form and the flash error message is displayed. Here is my code:
AppController的:
class AppController extends Controller
{
function beforeFilter()
{
$this->Auth->userModel = 'Users';
$this->Auth->fields = array('username' => 'email', 'password' => 'password'); //have to put both, even if we're just changing one
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'hotels', 'action' => 'dashboard');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
}
}
login.ctp:
<?php
echo $this->Form->create('User', array('action' => 'login'));
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->end('Login');
?>
UsersController:
class UsersController extends AppController
{
var $name = 'Users';
var $helpers = array('Html','Form');
var $components = array('Auth','Session');
function beforeFilter()
{
$this->Auth->allow("logout");
parent::beforeFilter();
}
function index() { } //Redirects to login()
function login()
{
if ($this->Auth->login())
{
$this->redirect($this->Auth->redirect());
} else
{
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
function logout()
{
$this->redirect($this->Auth->logout());
}
}
?>
我AP preciate任何帮助。谢谢!
I appreciate any help with this. Thanks!
推荐答案
的无效的用户名或密码,请重试的显示错误你打登录之后?
The "Invalid username or password, try again" error is displayed after you hit login?
有你应该检查几件事情:
There are a few things you should check:
•是输出 $这个 - &GT; Auth-&GT;登录()
等同于数据库中的信息?把调试($这个 - &GT; Auth-&GT;登录())。
来查看输出在您的登录方式提交表单后
• Is the output of $this->Auth->login()
identical to the information in your database? Put debug($this->Auth->login())
to see the output in your login method after the form is submitted.
•在数据库中正确地散列密码?
• Are the passwords correctly hashed in the database?
•尝试使 AuthComponent
适用于所有的控制器不只是 UsersController
。
• Try making the AuthComponent
available to all your controllers not just the UsersController
.
•不知道这是否有差别,但拨打父:: beforeFilter();
中的任何事情之前控制器的 beforeFilter
方法。
• Not sure if this makes a difference, but call parent::beforeFilter();
before anything else in your controller's beforeFilter
method.
编辑:
时看到你试图基于电子邮件地址和密码进行验证。作为默认AuthComponent需要一个用户名和密码。你必须明确说明您想要的电子邮件地址和密码通过 $这个 - &GT予以审定; Auth-&GT;登录()
。这个来自2.0文档:
Is see that you're trying to validate based on email and password. As a default AuthComponent expects a username and password. You have to explicitly state that you want the email and password to be validated by $this->Auth->login()
. This comes from the 2.0 documentation:
public $components = array(
'Auth'=> array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
这是你没有看到任何SQL输出的其实是可以预料的,我相信。
The fact that you're not seeing any SQL output is to be expected, I believe.
这篇关于不能得到验证登录使用CakePHP 2.0工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!