2012-02-16 9 views
9

私はUserモデルと認証システムをゼロから構築した単純なアプリを持っています。私は今、代わりにDeviseを使用しようとしています。それはうまくいかず、ウェブ開発の初心者として、私が把握していないものがあり、これをデバッグする方法がわかりません。Devise:サインインに失敗しました。デバッグ方法は?

私の問題は、両方のフィールドが正しい場合でも、サインインが常に「無効な電子メールまたはパスワード」を返すという点を除いて、Deviseがインストールされ、動作することです。

私は空のアプリを開発し、Deviseを追加しましたが、この問題はありません。この問題はおそらく、既存のUserモデルにDeviseを追加しようとするために起こります。

私はdocとDeviseのwikiを読んでいますが、このテーマに関するトピックはhereです。 Userモデルにはすでに電子メールフィールドがあるので、マイグレーションフィールドからdatabase_authenticatableを削除し、マイグレーションで行ったt.encrypted_pa​​sswordに置き換えます。

私のユーザモデルでは、database_authenticatableをattr_accessibleに残しました。私がそれを削除すると、session_pathが認識されないなどのエラーメッセージが多く出ますが、移行されていませんか?また、encrypted_pa​​sswordは私のモデルのどこにも表示されませんが、これは正常ですか... ...

私はそれが本当に初心者の質問であることを知っています。私は少し失われています。または簡単な修正がある場合...デバッグ方法もわかりません。ログに表示されるのは、ユーザーが正常にサインインする必要があるときに「認証されていません」と表示され、「authentication_token」が同じ...それはあなたに明白なようであれば、一度

を申し込んで生成したワンはそう、私は失われたよ、私は何かアドバイスを聞いて喜んでいるだろうとsign_inする

をしようとしたとき、私は下に追加routes.rb、User.rb、schema.rbおよび移行ファイル

routes.rbを:

TapaG::Application.routes.draw do 

    devise_for :users 

    get "pages/home" 

    resources :users 
    resources :belongings 

    devise_scope :user do 
    get "sign_in", :to => "devise/sessions#new" 
    get "sign_out", :to => "devise/sessions#destroy" 
    get "sign_up", :to => "devise/registrations#new" 
    end 

    get "pages/more_details" 
    get "pages/infos_pratiques" 
    get "pages/contact_us" 

    #match 'Profil', :to => 'users#Profil' 
    match "more_details", :to => "pages#more_details" 
    match 'contact_us', :to => 'pages#contact_us' 
    match "infos_pratiques", :to => "pages#infos_pratiques" 
    match '/belongings/new', :to => 'belongings#new' 
    root :to => 'pages#home' 

マイグレーション:

class AddDeviseToUsers < ActiveRecord::Migration 
    def self.up 
    change_table(:users) do |t| 
     t.recoverable 
     t.rememberable 
     t.trackable 
     t.encrypted_password :null => false, :default => '', :limit => 128 

     # t.encryptable 
     # t.confirmable 
     # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both 
     # t.token_authenticatable 


     # Uncomment below if timestamps were not included in your original model. 
     # t.timestamps 
    end 

    add_index :users, :email,    :unique => true 
    add_index :users, :reset_password_token, :unique => true 
    # add_index :users, :confirmation_token, :unique => true 
    # add_index :users, :unlock_token,   :unique => true 
    # add_index :users, :authentication_token, :unique => true 
    end 

    def self.down 
    # By default, we don't want to make any assumption about how to roll back a migration when your 
    # model already existed. Please edit below which fields you would like to remove in this migration. 
    raise ActiveRecord::IrreversibleMigration 
    end 
end 

User.rb:

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :registerable, #:database_authenticatable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me 
    attr_accessor :password 
    attr_accessible :name, :number_of_positive_reco, :confidence_percent, :avatar 

schema.rb:

create_table "users", :force => true do |t| 
    t.string "name" 
    t.string "email" 
    t.integer "number_of_positive_reco" 
    t.float "confidence_percent" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "encrypted_password" 
    t.string "salt" 
    t.string "avatar_file_name" 
    t.string "avatar_content_type" 
    t.integer "avatar_file_size" 
    t.datetime "avatar_updated_at" 
    t.boolean "admin",     :default => false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   :default => 0 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    end 

    add_index "users", ["email"], :name => "index_users_on_email", :unique => true 
    add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true 

+0

実際には、工夫が説明のために、今それを得た、ああ...おかげで – citraL

+0

を(ユーザーがサインアップした後の両方NILに設定されているencrypted_pa​​sswordと塩)任意のパスワードの暗号化を行いません:) – socjopata

答えて

9

答えは:attr_accessor:password ...を削除します。そうでなければ、Deviseは暗号化できません!

+2

を私は、まったく同じ問題を抱えていました私は 'attr_accessor:password'行を持っていませんでした。私の場合、問題は 'before_save:encrypt_password'があったために発生したので、私はパスワードを2回暗号化していました。 誰もが同じ問題に遭遇した場合に備えてここに残しておきます – Ramses

1

コンソールでは、このパスワードで暗号化可能とする方法である

u = User.last 
u.valid_password? 

を使用することができ、パスワードが所与のユーザーに対して有効であるかどうかを教えてくれます。

関連する問題