2012-01-01 15 views
0

CakePHP 2.0アプリケーションにログインしようとしていますが、ログインエラーが発生します。公式documentationで と私はパスワードをハッシュする方法を読んだが、私はまだログインエラーを取得し、ここで私はそれをやった方法ですtutorialパスワードと認証コンポーネントをハッシュする

:私はこれを持っているビューで

// Users Model 
public function beforeSave ($options = array()) { 
    $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']); 
    return true; 
} 

// Users Controller 
public $components = array ('Acl', 'Session', 
    'Auth' => array (
    'authenticate' => array (
     // login e logout sono di default i seguenti controller e views 
     // 'loginRedirect' => array ('controller' => 'users', 'action' => 'login'), 
     // 'logoutRedirect' => array ('controller' => 'users', 'action' => 'logout'), 
     'Form' => array (
      'fields' => array (
      // il valore default 
       'username' => 'email' 
      ), 
      'scope' => array (
       'User.active' => 1 
      ) 
     ) 
    ), 
    'authError' => 'Login error message I get' 
)); 

public function login() { 
    if ($this->request->is('post')) { // if the request came from post data and not via http (useful for security) 
     // the password is hashed in User Model in beforeSave method as read on documentation 
     // debug ($this->data); 
     if ($this->Auth->login()) { 
      $id = $this->Auth->user('id'); 
      return $this->redirect(array('controller'=>'users', 'action'=>$id, $this->Auth->user('username'))); 
     } else { 
      $this->Session->setFlash('Login error message', 'default', array(), 'auth'); 
     } 
    } 
} 

私はデータをデバッグしようとした場合

// the view login.ctp 
echo $this->Form->text('User.email', array('id'=>'email', 'value'=>'[email protected]')); 
echo $this->Form->password('User.password', array('id'=>'password', 'value'=>'password')); 

私はこれを取得:

// in the controller 
debug($this->data); 
// in the view 
Array 
(
    [User] => Array 
    (
     [email] => [email protected] 
     [password] => thepass // not hashed 
    ) 
) 

私はいつもLogin error messageを取得するので、私はログインできません。どうすれば修正できますか?

答えて

0

Vitto、

(1)エラーメッセージが表示されますか?

(2)レコードのために、レイアウトでsessionFlashが印刷されていることを確認してください!

echo $this->Layout->sessionFlash(); 

(3)認証コンポーネントはどのように設定しましたか?例えば、私のAppControllerに私はこの方法を設定しました:

public $components = array(
    'Session', 
    'Cookie', 
    'Acl', 
    /** 
    * Default is authorize option is ActionsAuthorize. 
    * In this case, system uses AclComponent to check for permissions on an action level. 
    * learn more: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#authorization 
    */ 
    'Auth'=> array(
     'authorize' => array(
      'Actions' => array('actionPath' => 'controllers') 
     ), 
     'authenticate' => array(
      'Form' => array(
       'fields' => array('username' => 'email', 'password' => 'password') 
      ) 
     ) 
    ) 
); 

(4)最後に、私は信じている(確認する必要があります)、手動でハッシュパスワードは、ログインを実行するためにする必要はありません。少なくとも、正しい設定の後、このコードは私のために働きます:

if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 
      // recirect stuffs 
+0

ありがとうございました1)質問のエラーを更新しました。 | 2)sessionFlashを印刷しようとすると次のようになります:致命的なエラー:/public_html/site.com/app/View/Layouts/default.ctpの非オブジェクトのメンバ関数sessionFlash()を呼び出します。 3)私はこれを見てみましょう!ありがとう – vitto

+0

(2)あなたのコントローラやAppControllerで '$ helpers'配列のヘルパー' Layout'を設定してください。 'public $ helpers = array(/ * others * /、 'Layout'、/ * others * /)'; – colares

+0

多分あなたが話しているかもしれません: '<?php echo $ this-> Session-> flash();エコー$ this-> Session-> flash( 'auth'); ?> 'これは動作していますが、エラーを取得するために使用しています – vitto

関連する問題