0

行き過ぎた質問であることは承知していますが、すべての回答で解決策を見つけることができませんでした。おそらくあなたは私を助けることができます。ユーザーをログインしようとすると、正しいデータで「ユーザー名/パスワードが無効です」というエラーが表示されます。CakePHP は初めての経験です。

コードに。

モデル:

App::uses('AppModel','Model');

Class User extends AppModel {

    public $useTable = 'Users';

    public $hasMany = array(
        'Costumer' => array(
            'className' => 'Costumer',
            'foreignKey' => 'users_id',
            'order' => 'Costumer.name ASC'
        )
    );

    //Suppressed the validation code, don't think it's important here

    public function beforeSave($options = array()){
        if (!empty($this->data['User']['pwd'])) {
            $this->data['User']['passwd'] = Security::hash($this->data['User']['pwd']);
        }
    }
}

コントローラ:

App::uses('AppController', 'Controller');

class UsersController extends AppController{

public $helpers = array('Html', 'Form');
public $name = 'Users';
public $components = array('Auth','Session');

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

public function login(){
    //Tests
    $userEmail = $this->User->findByEmail($this->request->data['User']['email']);
    $userPass = $this->User->findByPasswd(Security::hash($this->request->data['User']['passwd']));
    die(var_dump($userEmail, $userPass));

    if ($this->request->is('post')) {
        $this->request->data['User']['passwd'] = Security::hash($this->request->data['User']['passwd']);
        if ($this->Auth->login()){
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Session->setFlash(__('E-mail e/ou usuário incorretos, tente novamente.'));
        }
    }
}

意見:

<div class="row" style="margin-top: 40px;">
<div class="col-lg-8">

</div>
<div id="login" class="col-lg-2" style="background-color: #eee">
    <h3>Conecte-se.</h3>
    <?php 
        echo $this->Form->create('User', array(
            'label' => 'login', 
            'class' => 'form-horizontal form-group'
            )
        );
        echo $this->Form->input('email', array(
            'label' => 'E-mail',
            'class' => 'form-control',              
            )
        );
        echo $this->Form->input('passwd', array(
            'label' => 'Senha',
            'class' => 'form-control', 
            )
        );
        echo '<br />';
        echo $this->Form->end(array(
            'label' => 'Entrar', 
            'class' => 'btn btn-success'
            )
        );
     ?>
</div>

そして、私の AppController.php には次のものがあります。

class AppController extends Controller {
//public $components = array('DebugKit.Toolbar');

public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'pages', 'action' => 'display', 'homemk'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'homemk'),
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email', 'password' => 'passwd'),
                'passwordHasher' => 'Blowfish'
            )
        ),
        'authError' => 'Para visualizar esta página, você precisa estar logado.'
    )
);

public function beforeFilter(){
    $this->Auth->allow('display');      
    $this->set('authUser', $this->Auth->user());

}

}

クレイジーなことは、両方の UsersController の行です

$userEmail = $this->User->findByEmail($this->request->data['User']['email']);

$userPass = $this->User->findByPasswd(Security::hash($this->request->data['User']['passwd']));    

ログインしようとしているユーザーを返すので、データエラーではないようです。

彼ら!ここで何が欠けていますか?

ありがとう。

編集

これを「エレガントな」方法で行う方法が見つからなかったので、ダミーの回避策を書きました。request->data 値をデータベースに対して手動でチェックし、手動でユーザーをログインさせます。これは一時的な解決策です。後で説明します。

public function login(){
    if ($this->request->is('post')) {
        $user = $this->User->findByEmail($this->request->data['User']['email']);            
        if (!empty($user) && ($user['User']['passwd'] == Security::hash($this->request->data['User']['passwd']))){
            $this->Auth->login($this->request->data);
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Session->setFlash(__('E-mail e/ou usuário incorretos, tente novamente.'));
        }
    }
}
4

1 に答える 1