2017-05-24 43 views
0

我試圖按照食譜中的步驟執行身份驗證,但是看到Auth-> identify()函數仍然沒有向$ user變量返回任何內容,就像找不到憑據一樣。 我有一個名爲Usuarios,我想用戶的字段電子郵件contrasena(密碼)進行身份驗證。無法實現身份驗證 - CakePHP

這裏就是我所做的:

AppController.php

$this->loadComponent('Auth', [ 
    'authenticate' => [ 
     'Form' => ['finder' => 'auth', 
        'userModel' => 'Usuarios'] 
    ], 
    'loginAction' => [ 
     'controller' => 'Usuarios', 
     'action' => 'login' 
    ], 
    'unauthorizedRedirect' => $this->referer() 

]); 
$this->Auth->allow(['display']); 

UsuariosController.php

public function login() 
{ 
    if ($this->request->is('post')) { 
     $user = $this->Auth->identify(); 
     if ($user) { 
      $this->Auth->setUser($user); 
      return $this->redirect($this->Auth->redirectUrl()); 
     } 
     $this->Flash->error('Tu usuario o contraseña es incorrecta.'); 
    } 
} 

login.ctp

<h1>Login</h1> 
<?= $this->Form->create() ?> 
<?= $this->Form->control('email') ?> 
<?= $this->Form->control('password') ?> 
<?= $this->Form->button('Login') ?> 
<?= $this->Form->end() ?> 

UsuariosTable.php

public function findAuth(\Cake\ORM\Query $query, array $options) 
{ 
    $query 
     ->select(['id', 'email', 'contrasena']) 
     ->where(['Usuarios.estado' => 1]); 

    return $query; 
} 

你的幫助是非常感謝!

謝謝!

+0

問題解決了嗎? –

回答

0

你必須根據電子郵件用戶名做出一些改變:

public $components = array(
     'Auth' => array(
      'authenticate' => array(
       'Form' => array(
        'fields' => array('username' => 'email') 
       ) 
      ) 
     ) 
    ); 

https://bitbucket.org/snippets/eom/rLo49

+0

感謝這工作完美! +1 –

+0

@AndrésBadilla如果這個答案真的幫助你標記爲一個權利,所以可能對他人有幫助 –

0

是一個愚蠢的猜測

login.ctp

<?= $this->Form->control('password') ?> 

應該

<?= $this->Form->control('contrasena') ?> 
+0

是的,它是這樣的,但艾哈邁德的評論是正確的,並且鏈接非常準確,因爲我需要一些更改以正確適應西班牙語。 –