2011-08-06 7 views

答えて

4

「ユーザー名とメールアドレスの両方をユーザー名として使用する」とはどういう意味ですか?

編集:OK、Authはユーザの入力する「ユーザ名」と比較するためにdbのユーザ名と電子メールフィールドの両方を検索しますか?あなたはAUTHSのautoredirectをスキップして、それを自分で管理する必要がこれを行うには

function beforeFilter() { 
    parent::beforeFilter(); 
    $this->Auth->fields = array('username' => 'username', 'password' => 'pass'); 
    $this->Auth->autoRedirect = false; 
} 
function login(){ 
    if ($this->Auth->user()) { 
    $this->redirect($this->Auth->redirect()); 
    } else if (!empty($this->data)) { 
    $this->Auth->fields = array('username' => 'email', 'password' => 'pass'); 
    $this->data['User']['email'] = $this->data['User']['username']; 
    if($this->Auth->login($this->data))$this->redirect($this->Auth->redirect()); 
    } 
} 
+1

私は、ユーザ名または電子メールのどちらかを有効なフィールドとして使用してログインすると思っています – Stoosh

+0

電子メールアドレスまたはユーザ名のいずれかをフォームのユーザ名フィールドとして使用できます。 –

+0

上記のコードは機能しません。 –

3

:その後、これを行います。あなたのusers_controllerこのログインアクション:これは私のアプリから直接コピーされたので、あなたのための微調整のビットを必要とする場合があり

public function login() { 
    if(!empty($this->data)) { // Submitted form 

     // Try to login with Email 
     if(!$this->Auth->user() // if user wasn't logged in with username + pass 
      && !empty($this->Auth->data['User']['username']) 
      && !empty($this->Auth->data['User']['password']) 
     ) { 
      $user = $this->User->find('first', array(
       'conditions' => array(
        'User.email' => $this->Auth->data['User']['username'], 
        'User.password' => $this->Auth->data['User']['password'] 
       ), 
       'recursive' => -1 
      )); 

      if(!empty($user) && $this->Auth->login($user)) { 
       // They logged in, so kill the flash error message 
       $this->Session->delete('Message.auth'); 
      } else { 
       $this->Session->setFlash($this->Auth->loginError, $this->Auth->flashElement, array(), 'auth'); 
      } 
     } 

     if($this->Auth->user()) { 
      // Post login logic here 
      $this->redirect($this->Auth->redirect()); 
     } 

    } else { 
     if($this->Auth->user()) { 
      $this->Session->setFlash(__d('users', 'You are already registered and logged in!', true)); 
      //$this->redirect('/'); 
      $this->redirect($this->Auth->redirect()); 
     } 
    } 

。あなたのAppControllerに$this->Auth->autoRedirect = false;を設定することを忘れないでください:beforeFilter();

Authはユーザー名とパスワードを自動的にチェックするので、このアクションはそのことを覚えておく必要があります。 Session::remove()コールは、ユーザー名/パスワードのチェックに失敗したときにAuthエラーメッセージを自動的に削除することです。そうでなければ、成功したログインでエラーメッセージが表示されます。

+1

thaks助けをたくさん –

関連する問題