ruby - 带有设计的自定义密码字段( ruby )

标签 ruby ruby-on-rails-3.1 devise rubygems

我正在使用 2 个 Rails 应用程序共享的数据库。

使用 BCrypt 和 has_secure_password 对用户进行身份验证的网络应用程序,以及我的应用程序,一个 REST API,使用 Devise 对用户进行身份验证。密码哈希值相同。

所以,我想使用字段 password_digest 而不是 encrypted_pa​​ssword 来通过 Devise 进行身份验证,但我不知道该怎么做! (我在文档中寻找但一无所获)。所以,我必须将我的密码哈希从 password_digest 复制/粘贴到 encrypted_pa​​ssword。

这里是我的 session Controller 代码:

class SessionsController < Devise::SessionsController

before_filter :ensure_params_exist

def create
    build_resource
    resource = User.find_for_database_authentication(:email => params[:email])
    return invalid_login_attempt unless resource

    if resource.valid_password?(params[:password])
        #resource.ensure_authentication_token!  #make sure the user has a token generated
        sign_in("user", resource)
        render :json => { :authentication_token => resource.authentication_token, :lastname => resource.lastname, :firstname => resource.firstname, :last_sign_in => resource.last_sign_in_at }, :status => :created
    return
    end
    invalid_login_attempt
end

#def destroy
#   # expire auth token
#   @user=User.where(:authentication_token=>params[:auth_token]).first
#   @user.reset_authentication_token!
#   render :json => { :message => ["Session deleted."] },  :success => true, :status => :ok
#end


protected
    def ensure_params_exist
        return unless params[:email].blank?
        render :json=>{:success=>false, :message=>"missing email parameter"}, :status=>422
    end

    def invalid_login_attempt
        warden.custom_failure!
        render :json => { :errors => ["Invalid email or password."] },  :success => false, :status => :unauthorized
    end

结束

然后是我的用户模型

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

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :client_id, :firstname, :group_id, :lastname, :password, :password_confirmation, :role_id, :group_ids, :auth_token, :password_digest, :encrypted_password

  # Relations dans la base de données
  belongs_to :client
  belongs_to :role

  has_many :memberships
  has_many :groups, :through => :memberships



end

最佳答案

我不知道 BCrypt/has_secure_password 是如何工作的,但你可以 如下使用虚拟属性

def encrypted_password
 return password_digest
end

def encrypted_password= value
 return password_digest
end

或者更好的是,使用别名方法 将 encrypted_pa​​ssword 和 encrypted_pa​​ssword= 设置为 password_digest 和 password_digest= 的别名方法。

关于ruby - 带有设计的自定义密码字段( ruby ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15828045/

相关文章:

ruby-on-rails - rails 3.1 中的嵌套表单

ruby-on-rails - Rails/devise如何处理Cookie session ?

ruby-on-rails - 如何在 Rails 4 中正确设置 database.yml 文件

java - 一种在 javascript 上使用两个输入(字符串)获取哈希键的方法?

ruby-on-rails - 选择插件: Multiple select menu prompt not showing?

ruby-on-rails - 如何在 HABTM 中使用接受嵌套属性?

ruby-on-rails - RoR 的第二天,运行服务器出现问题

ruby - Splat 参数与 Espresso

ruby-on-rails - rails s 在设计配置发送邮件时出错

ruby-on-rails - rails 3/设备 : Checking to see if users have any of multiple roles