Login Script in 2.4.2 is not working

后端 未结 2 1295
终归单人心
终归单人心 2020-12-07 06:13

I am new to cakephp. I have a problom while login. With wrong name and password redirects to login home page.

UsersController.php

public function          


        
相关标签:
2条回答
  • 2020-12-07 06:52

    You are using AuthComponent::login() wrong, you are only supposed to pass data to it in case you want to manually login a user, ie without automatic authentication.

    If you want to use the components authentication functionality just call $this->Auth->login()

    See also: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in

    In 2.x $this->Auth->login($this->request->data) will log the user in with whatever data is posted, whereas in 1.3 $this->Auth->login($this->data) would try to identify the user first and only log in when successful.

    0 讨论(0)
  • 2020-12-07 07:00
    usercontroller
    
    public function beforeFilter() {
            parent::beforeFilter();
            $this->Auth->allow('login','logout');
        }
    
    
    
    public function login()
        {
           $this->layout= 'login';
    
           if ($this->request->is('post')) {
    
                if ($this->Auth->login()) {
    
                            $this->redirect('/users');
                            else {
    
                    $this->Session->setFlash(__('Invalid email or password, please try again'));
    
                }
            }
            else{
                if($this->Auth->loggedIn())
                    $this->redirect('index');
            }
        }
    }
    

    AppController

    class AppController extends Controller {
    
    public $components = array(
    
        'Session',
        'Auth' => array(
            'authenticate' => array(
                'Form' => array( 
                    'userModel' => 'User',
                     'fields' => array(
                        'username' => 'user_name',
                        'password' => 'password'
                        )
                    )
                ),
            'loginAction' => array('controller' => 'users', 'action' => 'login'),
                        'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
            'authError' => 'You don\'t have access here.',
                    /*
                    'loginAction' => array('controller' => 'users', 'action' => 'forgot_password'),
                    'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
                    'logoutRedirect' => array('controller' => 'users', 'action' => 'forgot_password'),
                    'authError' => 'You don\'t have access here.',
                    */
            ),
    
    );
    
    0 讨论(0)
提交回复
热议问题