2017-07-28 206 views
0

这段代码有什么问题? 控制器和标识只返回false! 我的数据库列是senha(密码)和电子邮件。我无法登录。 我使用散列,密码为255个字符,并且都是正确的。 但它不工作!Cakephp 3 - Auth登录返回False

ContaController.php

public function initialize() { 
    $this->loadComponent('Flash'); 
    $this->loadComponent('Auth', [ 
     'authenticate' => [ 
      'Form' => [ 
       'fields' => ['email' => 'email', 'senha' => 'senha'], 
       'userModel' => 'Conta', 
       'finder' => 'auth', 
      ] 
     ], 
     'authorize' => ['Controller'], 
     'loginAction' => [ 
      'controller' => 'Conta', 
      'action' => 'index', 
     ], 
     'loginRedirect' => [ 
      'controller' => 'Conta', 
      'action' => 'minha-agenda' 
     ], 
     'logoutRedirect' => [ 
      'controller' => 'Conta', 
      'action' => 'index', 
     ], 
     'storage' => 'Memory' 
    ]); 
    $this->Auth->allow(['index']); 
} 


public function index() { 
    if ($this->request->is('ajax') || $this->request->is('post')) { 
     $user = $this->Auth->identify(); 
     if ($user) { 
      $this->Auth->setUser($user); 
      // return $this->redirect($this->Auth->redirectUrl()); 
      echo 'success'; 
     } else { 
      var_dump($user); 
      echo 'incorrect'; 

     } 
    } 
} 

ContaTable.php

public function initialize(array $config) { 
    parent::initialize($config); 

    $this->setTable('alunos'); 
    $this->setDisplayField('id'); 
    $this->setPrimaryKey('id'); 

    $this->addBehavior('Timestamp'); 

    $this->belongsToMany('Alunos', [ 
     'foreignKey' => 'interesses_id', 
     'targetForeignKey' => 'alunos_id', 
     'joinTable' => 'alunos_interesses' 
    ]); 
} 

public function validationDefault(Validator $validator) { 
    $validator 
     ->notEmpty('email', 'A username is required') 
     ->notEmpty('senha', 'A password is required'); 
    return $validator; 
} 

public function findAuth(\Cake\ORM\Query $query, array $options) { 
    $query 
     ->select(['id', 'email', 'senha']) 
     ->where(['Conta.email' => $options['email']]) 
     ->andWhere(['Conta.senha' => $options['senha']]); 
    return $query; 
} 

我需要帮助解决这个问题。 数据库中的列是不同的,所以我不打算将它用作默认值。 更进一步,它将成为ajax,但现在它是如此,因为我解决不了!

+0

此网站是StackOverflow英文网站。要使用葡萄牙语网站,请访问https://pt.stackoverflow.com/ –

+0

葡萄牙语和英语都可以解决......巴西人和葡萄牙人不会非常使用这个网站!这就是我用英文写作的原因。任何问题都会在Google翻译中输入文字! –

+0

你的.ctp文件是什么? –

回答

1

变化'fields' => ['email' => 'email', 'senha' => 'senha']'fields' => ['username' => 'email', 'password' => 'senha']

有了这个配置,你不需要自定义取景器,除非你需要选择列。通过使用默认查找器,您可以始终取消设置属性。

确保您的登录表单控件和表列被命名为emailsenha

然后,我想知道,如果你eighter方式应该在AppController中加载authcomponent。

相关问题