Ruby on rails 设备无法登录
我有一个简单的用户模型Ruby on rails 设备无法登录,ruby-on-rails,Ruby On Rails,我有一个简单的用户模型 class User < ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, # :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerab
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,:confirmable, :validatable
include BCrypt
#attr_accessor :password, :password_confirmation
has_many :deals
has_many :charges
has_one :menu
has_many :vouchers
has_one :authentication
has_many :restaurant_tags
has_many :restaurant_hours
#validates :login, uniqueness: true
#validates :login, presence: true
validates :password, presence: { on: :create }
validates :password, confirmation: true
#validates :login, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create, message: "must be a valid email address."}
validates :role, inclusion: { in: ["admin", "client", "restaurant"] }
#before_save :encrypt_password
def encrypt_password
if password.present?
self.salt = BCrypt::Engine.generate_salt
self.crypted_password = BCrypt::Engine.hash_secret(password, salt)
end
end
def self.authenticate(login, password)
user = find_by_login(login)
if user && user.crypted_password == BCrypt::Engine.hash_secret(password, user.salt)
user
else
nil
end
end
def restaurant?
role == "restaurant"
end
def client?
role == "client"
end
def admin?
role == "admin"
end
end
我可以成功注册,确认后,我可以看到我已登录,但当我注销,然后尝试登录时,我面临的错误在2毫秒内完成。我已经花了一整天的时间在这个问题上了,请帮我解决。谢谢。请更新这个方法
def self.authenticate(login, password)
user = self.find_by_login(login)
if user && user.crypted_password == BCrypt::Engine.hash_secret(password, user.salt)
user
else
nil
end
end
我已经解决了这个问题,当我在我刚刚更新的视图中使用电子邮件时,设备正在检查登录字段以进行身份验证
config.authentication_keys = [:email]
config.case_insensitive_keys = [:email]
config.strip_whitespace_keys = [:email]
这就解决了我的问题谢谢你的帮助我知道你在保存:加密密码行之前已经注释掉了
,那么你是从其他地方调用这个方法的吗?如果没有,我想authenticate
方法确实会失败,因为原始密码没有使用BCrypt
引擎加密(因此加密的密码不匹配)。有可能吗?你是对的,但是自动设计加密密码我们不需要加密密码。从任何地方删除或评论你的自定义身份验证,使用Desive u不需要这样做…并且删除“include bcrypt”也..我不知道你为什么要自己加密密码?这一切都是由Desive?@Deva完成的,我禁用了它,这意味着我没有调用这个方法35;在_save:encrypt_password之前
config.authentication_keys = [:email]
config.case_insensitive_keys = [:email]
config.strip_whitespace_keys = [:email]