2012-02-16 113 views
9

我有一個用戶模型和從零開始構建的身份驗證系統的簡單應用程序。我現在試圖使用Devise來代替它,它不起作用,並且作爲web開發中的新手,有一些我沒有掌握,我不知道如何調試。設計:登錄失敗,如何調試?

我的問題是:設計安裝和工作,除非登錄始終返回「無效的電子郵件或密碼」,即使這兩個字段都是正確的。

我開發了一個空應用程序,添加了Devise,並且我沒有這個問題。因此,問題可能是因爲我試圖將Devise添加到現有的用戶模型。

我已閱讀doc和Devise wiki,其中有一個關於此主題的主題:here。說從遷移字段中刪除:database_authenticatable,因爲用戶模型已經有一個電子郵件字段,並用t.encrypted_pa​​ssword替換它,這是我在遷移中所做的。

現在,在我的用戶模型中,我離開了:attr_accessible中的database_authenticatable。如果我刪除它,我有很多錯誤消息,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

實際上,Devise不會做任何密碼加密(用戶註冊後加密密碼和salt都設置爲NIL)... – citraL 2012-02-16 13:29:01

+0

啊,現在明白了,謝謝你的解釋:) – socjopata 2012-02-16 21:43:05

回答

9

答案是:刪除attr_accessor:密碼...否則設計不能加密它!

+2

我有完全相同的問題,只有我沒有'attr_accessor:password'行。在我的情況下,問題是由於我有'before_save:encrypt_password',所以我加密了兩次密碼。 如果有人遇到同樣的問題,我會把它留在這裏 – Ramses 2012-11-14 22:40:51

1

在控制檯中可以使用

u = User.last 
u.valid_password? 

這是密碼,可加密的方法,並會告訴你,如果密碼是有效的給予用戶。