2012-12-27 23 views
0

我正在嘗試爲Facebook用戶創建認證。現在我檢查一下我的數據庫中是否存在一個fb用戶標識,如果它確實存在,那麼它會認證,如果沒有,那麼它會挖掘用戶的facebook信息並創建一個用戶,然後進行身份驗證。它工作成功,問題是,如果我使用類似$this->Auth->user('id');的值返回空值。我很好奇我可能做錯了什麼。以下是我的代碼CakePHP 2.2.4使用替代認證後不保存Auth用戶信息

public function fb_authenticate($data) { 
    $this->Auth->fields = array('username' => 'fbid', 'password' => 'fbpassword'); 
    $this->loadModel('User'); 
    $user_record = $this->User->find('first', array(
      'conditions' => array('fbid' => $data['user_id']) 
    )); 
    if(empty($user_record)) { 
     $fbu = $this->Facebook->getUserInfo($data['user_id']); 
     $user_record = array(
      'User'=>array(
       'username'=>$fbu->username, 
       'fbid'=>$data['user_id'], 
       'oauth_token'=>$data['oauth_token'], 
       'access_token'=>$data['access_token'], 
       'firstname'=>$fbu->first_name, 
       'lastname'=>$fbu->last_name, 
       'fbpassword'=>$this->Auth->password($data['user_id']), 
       'role'=>'user' 
     ));  
     $this->User->create(); 
     $this->User->save($user_record,null); 
    } 
    if (!$this->Auth->login($user_record)) { 
     $this->Session->setFlash(__('Invalid username or password, try again')); 
    } 
} 

它驗證並讓用戶進入,但它不會將用戶信息存儲在Auth組件會話中。可能是什麼問題呢 ??

如果我調試debug($this->Auth->user())我可以看到數據,但如果我單獨拉字段debug($this->Auth->user('id'));它返回null

回答

2

變化$this->Auth->login($user_record)

$this->Auth->login($user_record['User'])

+0

這樣做。前幾天有同樣的問題 –

+0

@ numerical25 Btw在創建新用戶的情況下,您傳遞給login()的用戶數組沒有'id'鍵,請修復該問題。 – ADmad