简体   繁体   中英

Incorrect password or username in login form in cakephp

I don't know why am still getting invalid username or password while I have all the codes and my database. Also the user is already registered but cannot login.

AppController.php

class AppController extends Controller{

    public $components = array(
        /*'DebugKit.Toolbar',*/
        'Session',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'jobs', 'action' => 'index'),
            'logoutRedirect' => array(
                'controller' => 'users',
                'action' => 'login'
            )
        )
    );
}

Model: User.php

App::uses('AppModel', 'Model');
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {

    public $validate = array(
        'Username' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
            'The username already exists'=>array(
                'rule'=>array('isUnique'),
                'message'=>'The username already exists!'
            ),
        ),
        'Password' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
            'Match passwords'=>array(
                'rule'=>'matchPasswords',
                'message'=>'The password does not match'
            ),
        ),
        'Confirm_Password' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
    );


    function matchPasswords($data){

        if ($data['Password'] == $this->data['User']['Confirm_Password']) {

            return TRUE;
        }
        $this->invalidate('Confirm_Password', 'The password does not match');
        return FALSE;
    }

    public function beforeSave($options = array()) {
        if (!parent::beforeSave($options)) {
            return false;
        }
        if (isset($this->data[$this->alias]['Password'])) {
            $hasher = new SimplePasswordHasher();
            $this->data[$this->alias]['Password'] = $hasher->hash($this->data[$this->alias]['Password']);
        }
        return true;
    } 
}

Controller: UsersController.php

class UsersController extends AppController {

    public $components = array('Session');    

    var $name = 'Users';
    var $helpers = array('Form');

    // Placeholder for login_form, required by CakePHP to see the login_form view
    function login_form() { }

    public function beforeFilter () {
        parent::beforeFilter();
        $this->Auth->allow('user', 'login');
    }

    public function login(){
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Session->setFlash(__('Incorrect username or password'));
        }
    }

    public function logout() {

        return $this->redirect($this->Auth->logout());
    }

    public function done() {

        $this->set('framework', 'CakePHP');

    }

    public function user(){

        if($this->request->is('post')) {

            $this->User->create();

            if($this->User->save($this->request->data)) {
                $this->Session->setFlash('The information has been sent successfully!');
                return $this->redirect(array('action' => 'login'));

                $this->redirect('done');
            }
        }
    }
}

View: login.ctp

<h4>LOGIN HERE</h4>
    <?php
    echo $this->Session->flash('auth');
    echo $this->Form->create('User');
    echo $this->Form->input('Username');
    echo $this->Form->input('Password',array('type' => 'password'));
    echo $this->Form->end('Login');

    ?>

You are not using the default field names CakePHP is expecting. Therefore, you have to include them in your AuthComponent configuration.

Try the following:

class AppController extends Controller
{
    public $components = array(
        /*'DebugKit.Toolbar',*/
        'Session',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'jobs', 'action' => 'index'),
            'logoutRedirect' => array(
                'controller' => 'users',
                'action' => 'login'
            ),
            'authenticate' => array(
                'Form' => array(
                    'fields' => array(
                        'username' => 'Username',
                        'password'=>'Password'
                    ),
                ),
            )
        )
    );
}

These should match the field names in your users table and in your views.

EDIT

Also, make sure your password field in the database is long enough to store the hashed password. The default algorithm used by CakePHP is SHA1, which produces a 160 bit (40 character) hash. A VARCHAR 255 should be more than enough to store this string.

See:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM