Tôi có một ứng dụng đơn giản với mô hình Người dùng và hệ thống xác thực được xây dựng từ đầu. Tôi bây giờ đang cố gắng sử dụng Devise thay vào đó, và nó không hoạt động và, như một newbie trong phát triển web, có cái gì đó tôi không nắm bắt và tôi không biết làm thế nào để gỡ lỗi này.Thiết lập: Đăng nhập không thành công, cách gỡ lỗi?
Vấn đề của tôi là: Devise được cài đặt và hoạt động, ngoại trừ việc đăng nhập luôn trả lại "email hoặc mật khẩu không hợp lệ" ngay cả khi cả hai trường đều chính xác.
Tôi đã phát triển một ứng dụng trống, thêm Devise và tôi không gặp phải vấn đề này. Vấn đề này có thể xảy ra bởi vì tôi cố gắng thêm Devise vào một mô hình User hiện có.
Tôi đã đọc tài liệu và wiki Devise, có chủ đề về chủ đề này: here. nói để xóa: database_authenticatable từ các trường di chuyển, bởi vì Mô hình người dùng đã có trường email và thay thế bằng t.encrypted_password, mà tôi đã làm trong quá trình di chuyển.
Bây giờ, trong mô hình người dùng của tôi, tôi đã để lại: database_authenticatable trong attr_accessible. Nếu tôi xóa nó, tôi có rất nhiều thông báo lỗi rằng session_path không được nhận dạng, vv ... nhưng nó chưa được di chuyển? Ngoài ra,: encrypted_password không xuất hiện ở bất kỳ đâu trong mô hình của tôi, điều này có bình thường không? ...
Tôi biết nó thực sự là một câu hỏi mới, tôi hơi lạc mất và không biết tôi có nên viết lại ứng dụng của mình không bắt đầu hoặc nếu có một sửa chữa dễ dàng ... Tôi không biết làm thế nào để gỡ lỗi cũng có, tất cả tôi thấy trong các bản ghi là "trái phép" xuất hiện khi người dùng nên đăng nhập thành công, và cũng rằng "authentication_token" không phải là tương tự khi cố gắng để sign_in rằng một trong những tạo ra một lần đăng ký
như vậy, tôi bị mất, nếu nó có vẻ rõ ràng cho bạn, tôi sẽ rất vui khi nghe lời khuyên nào ...
tôi thêm bên dưới routes.rb, User.rb, schema.rb và tệp di chuyển
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'
di cư:
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
Thực ra, Devise không thực hiện bất kỳ mã hóa mật khẩu nào (encrypted_password và muối đều được đặt ở NIL sau khi người dùng đăng ký) ... – citraL
Ahhh, đã nhận ngay bây giờ, cảm ơn lời giải thích :) – socjopata