【问题标题】:Custom password field with devise (ruby)带有设计的自定义密码字段(红宝石)
【发布时间】:2013-04-05 07:19:16
【问题描述】:

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

使用 BCrypt 和 has_secure_password 对用户进行身份验证的 web 应用程序,以及我的应用程序,一个 REST API,使用 Devise 对用户进行身份验证。密码哈希是一样的。

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

这是我的会话控制器代码:

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

【问题讨论】:

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


    【解决方案1】:

    我不知道 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= 的别名方法。

    【讨论】:

    • 您好,感谢您的回答。我应该把这段代码放在哪里?我在我的用户模型中尝试过,但它返回一个错误,并且在我的会话控制器中,如果我尝试此代码,身份验证将不起作用。谢谢
    • 在用户模型中是。你得到了什么错误?您提到了一些关于将 password_digest 复制并粘贴到加密密码中的内容。一点细节。
    【解决方案2】:

    我认为完成这项工作的最佳方法是使用alias_attribute。这个问题可能很久以前就解决了,但我仍然想提出这个问题。

    alias_attribute :encrypted_password, :password_digest
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 2013-06-17
      相关资源
      最近更新 更多