2012-12-07 74 views
0

我知道這個問題已經被問過,但我已經通過幾乎每一篇文章我能找到在互聯網和沒事就消失瞭解決我的問題-.-登錄CakePHP中使用電子郵件和密碼2.1

我試圖讓我的CakePHP 2.1應用程序使用他們的電子郵件(而不是用戶名)和密碼登錄用戶。另外,考慮到我使用的控制器稱爲「配置文件」,而不是「用戶」。這有什麼區別嗎?總之,這裏是我的代碼...

AppController.php

<?php 
class AppController extends Controller { 
    public $components = array(
     'Session', 
     'Auth'=>array(
      'authorize => array('Controller'), 
      'Form'=>array(
       'fields' => array('username' => 'email'), 
       'userModel' => 'Profile' 
      ) 
     ) 
    ); 
} 
?> 

ProfilesController.php

<?php 
class ProfilesController extends AppController { 

    function login() { 
     if ($this->request->is('post')) { 
      if ($this->Auth->login()) { 
       $this->Session->setFlash('You have been signed in.', 'flash_success'); 
       $this->redirect($this->Auth->redirect()); 
      } else { 
       $this->Session->setFlash('Login failed. Your username/password was incorect.'); 
      } 
     } 
    } 

} 
?> 

login.ctp

<?php 
echo $this->Form->create(); 
echo $this->Form->input('email'); 
echo $this->Form->input('password'); 
echo $this->Form->submit('Sign In'); 
?> 

任何意見/建議,爲什麼這仍然登錄不工作!?我很困惑。

謝謝!

回答

1

謝謝您的回答!我剛剛解決了我的問題,這實際上是一個很容易犯的錯誤。

實際上,我是用:

'Form' => array(
    'userModel'=>'Profile', 
    'fields' => array('username' => 'email') 
) 

本身,而不是使用像這樣的 '身份驗證' 參數中的:

'authenticate' => array(
    'Form' => array(
     'userModel'=>'Profile', 
     'fields' => array('username' => 'email') 
    ) 
) 

感謝您的幫助傢伙!欣賞它。

2

你寫了這個函數嗎?

ProfilesController.php

function beforeFilter() { 
    $this->Auth->fields = array('username' => 'email', 'password' => 'password'); 
} 
+0

嘿那裏:),謝謝你回到我身邊!我已經在我的AppController.php中,我也只是把它放在我的ProfilesController.php以及...仍然沒有區別。它不斷返回我的「登錄失敗」的消息!任何其他想法? –

+0

「登錄失敗」?這可能是密碼問題... 檢查您的表單發送的密碼字符串以及數據庫中的值。他們是否是實例? – Matthieu

+0

是的,密碼都很好。我使用我的註冊表單創建了3個額外的用戶帳戶,並且他們都正確地進行了散列處理。仍然沒有登錄。 –

2

檢查俺們我給了類似的問題

unable to login in cakephp using different table

回來,如果這不能解決

+0

嘿,夥伴。它一開始看起來並不樂觀,但您的答案實際上讓我的用戶登錄!檢查我的答案,爲什麼我的登錄不起作用。真的很感激它,謝謝你! –

+0

歡迎亞當! :) – huzefam

相關問題