开发者

CakePHP - Login from home page using Auth

I decided to put my login form into an element and render it on my home page.

Logging in with correct credentials works just fine. Problem arises when incorrect credentials are given. After submitting the form, I get a 404 error. If I refresh the page though, the controller renders the login action just fine.

<?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 su开发者_Go百科ccesfully 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());
}


}

?>

Login element rendered in home page: (You can ignore 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));

Thanks!


Add the Auth component and Session component to your Users Controller.

var $components = array('Security','Auth','Session');

Actually, you can remove the Security component since you're not using $this->Security methods anywhere. If still not working, Specify the login action explicitly during component declaration:

var $components = array(
                        'Auth' => array(
                                       'loginAction' => array(
                                                         'controller' => 'users',
                                                             'action' => 'login',               
                                                             'plugin' => false,                       
                                                              'admin' => false)))


I removed the Security component from my user model and everything started working...

I don't know if this is expected functionality or a bug.

Cheers!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜