2012-02-26 17 views
0

ユーザーが正しい電子メールとパスワードでデータベースに存在していても、User.authenticateメソッドはnilを返します。これは、セッションコントローラまたはRailsコンソール(irb)のCreateアクションからauthenticateメソッドを呼び出すときに発生します。User.authenticateメソッドはRails 3でnilを返しますか?

この問題に関するお手伝いをさせていただきます。ここで

class SessionsController < ApplicationController 

    def new 
    end 

    def create 
     user = User.authenticate(params[:session][:email], 
            params[:session][:password]) 
     if user.nil? 
     flash.now[:error] = "Invalid email/password combination" 
     render 'new' 
     else 
     sign_in user 
     redirect_to user 
     end  
    end 

    def destroy 
     sign_out 
     render 'pages/options' 
    end 

end 

は私のUserモデルである:

class User < ActiveRecord::Base 

    attr_accessor :password 
    attr_accessible :first_name, :last_name, :email, :password, :password_confirmation,  
        :account_type, :email_confirmed, :weight 

    validates :password, :presence => true, 
        :confirmation => true, 
        :length => { :within => 6..40 } 

    before_save :encrypt_password 

    def has_password?(submitted_password) 
    encrypted_password == encrypt(submitted_password) 
    end 

    def self.authenticate(email, submitted_password) 
    user = find_by_email(email) 
    return nil if user.nil? 
    return user if user.has_password?(submitted_password) 
    end 

    def self.authenticate_with_salt(id, cookie_salt) 
    user = find_by_id(id) 
    (user && user.salt == cookie_salt) ? user : nil 
    end 

    private ################################################# 

    def encrypt_password 
    self.salt = make_salt if new_record? 
    self.encrypted_password = encrypt(password) 
    end 

    def encrypt(string) 
    secure_hash("#{salt}--#{string}") 
    end 

    def make_salt 
    secure_hash("#{Time.now.utc}--#{password}") 
    end 

    def secure_hash(string) 
    Digest::SHA2.hexdigest(string) 
    end 

    def generate_email_conf_code 
    email_conf_code = secure_hash("#{Time.now.utc}") 
    self.email_conf_code = email_conf_code 
    end 

end 
+0

これを絞り込み、 'find_by_email(email)'または 'user.has_password?(submitted_pa​​ssword)'がnilを返すかどうかを判断しましたか? – James

+1

'params'は' nil'を取得したときのように見えますか?どのメールとパスワードが一致すると思われますか? –

+0

user.has_password?(submitted_pa​​ssword)はnilを返します。私はまだ理由を理解できません。適切なパラメータがuser.authenticateに送信されます。私はデバッグページにparamsをダンプすることでそれを確認しました。 –

答えて

0

サーバーのログをチェックしてください。端末で直接監視することもできます。サーバー上で受信したセッション電子メールを探します。これはRails 3.1.1のようなものです。

Parameters: {"session"=>{"email"=>"[email protected]", "password"=>"[FILTERED]"}} 

メールが正しく届いていることを確認してください。そうでなければ、何をすべきか分かっていると思います。

0

データベースにpassword_digestまたはencrypted_pa​​ssword列が保存されていますか? michael hartlの古いチュートリアルではpassword_digestを使用していましたが、encrypted_pa​​sswordのようです。

関連する問題