我决定将我的登录表单放入一个元素并在我的主页上呈现它。
使用正确的凭据登录工作正常。给出不正确的凭据时出现问题。提交表单后,我收到404错误。如果我刷新页面,控制器会使登录操作正常。
<?php
class UsersController extends AppController {
var $name = 'Users';
var $components = array('Security');
// set redirect page after being logged out
function beforeFilter() {
$this->Auth->fields = array(
'username' => 'email',
'password' => 'password'
);
$this->Auth->allow('register');
$this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'map');
$this->Auth->logoutRedirect = array('controller' => 'posts', 'action' => 'map');
}
function register() {
$this->set('title_for_layout', 'Register');
if (!empty($this->data)) {
if ($this->User->save($this->data)) {
// after succesfully creating the user, log him in and redirect to map view
// for some reason, the hashed password is not kept after the save,
// therefore we must rehash the password
$this->data['User']['password'] = Security::hash($this->data['User']['tmp_password'], NULL, true);
$this->Auth->login($this->data);
$this->redirect(array('controller' => 'posts', 'action' => 'map'));
}
}
}
function login() {
$this->set('title_for_layout', 'Login');
}
function logout() {
$this->redirect($this->Auth->logout());
}
}
?>
在主页中呈现的登录元素:(您可以忽略JavaScript)
echo "<script> $(function() {
$('#UserDummyEmail').focus(function() {
$('#UserDummyEmail').hide();
$('#UserEmail').focus();
});
$('#UserEmail').blur(function() {
if (($('#UserEmail').val()).length == 0) {
$('#UserDummyEmail').show();
}
});
$('#UserDummyPassword').focus(function() {
$('#UserDummyPassword').hide();
$('#UserPassword').focus();
});
// fix for tabbing bug
$('#UserPassword').focus(function() {
$('#UserDummyPassword').hide();
});
$('#UserEmail').focus(function() {
$('#UserDummyEmail').hide();
});
$('#UserPassword').blur(function() {
if (($('#UserPassword').val()).length == 0) {
$('#UserDummyPassword').show();
}
});
});</script>";
echo $session->flash('auth');
echo $this->Form->create('User', array('action' => 'login'));
echo $this->Form->input('dummyEmail', array('div'=>false, 'label' => false, 'value' => 'Email', 'tabindex' => '1'));
echo $this->Form->input('dummyPassword', array('div'=>false, 'label' => false, 'value' => 'Password', 'tabindex' => '2'));
echo $this->Form->input('email', array('div'=>false, 'label'=>false, 'tabindex'=> '3'));
echo $this->Form->input('password', array('div'=>false, 'label'=>false, 'tabindex' => '4'));
echo $this->Form->end('Login', array('div'=>false));
谢谢!
答案 0 :(得分:0)
将Auth组件和会话组件添加到用户控制器。
var $components = array('Security','Auth','Session');
实际上,您可以删除安全组件,因为您没有在任何地方使用$ this-&gt;安全方法。如果仍然无法正常工作,请在组件声明期间明确指定登录操作:
var $components = array(
'Auth' => array(
'loginAction' => array(
'controller' => 'users',
'action' => 'login',
'plugin' => false,
'admin' => false)))
答案 1 :(得分:0)
我从用户模型中删除了安全组件,一切都开始工作......
我不知道这是预期的功能还是错误。
干杯!