【问题标题】:Is it possible to have a few passwords in authlogic gem?authlogic gem 中是否可以有几个密码?
【发布时间】:2020-08-06 20:24:42
【问题描述】:

现在我们有电话号码作为登录名,短信代码(4 位)作为密码。 当用户想登录时:

- user enters phone number 
- we generate code
- we save code to user password field
- we send code to user via sms
- user uses this sms code to login in

我们希望能够让最后 3 个生成的代码(密码)对登录有效:

- we started to save generated codes in separate table

问题是:如何将其连接到 authlogic?是否有任何回调关闭默认密码检查并让我能够添加我的自定义密码检查逻辑?

【问题讨论】:

  • 我们有 authlogic 3.4.6

标签: ruby-on-rails ruby authentication authlogic


【解决方案1】:

我找到了一个帮助我调整密码验证逻辑的解决方案。 我的authlogic 3.5.6 版和我在下面的实现中有一个名为validate_by_password 的方法。 我复制了它的第一部分以保存空白字段和​​逻辑检查。并以我需要的方式覆盖了无效的密码检查。

class Client::Session < Authlogic::Session::Base

  ...

  def validate_by_password
    # copy paste from gem
    self.invalid_password = false

    # check for blank fields
    errors.add(login_field, I18n.t('error_messages.login_blank', default: 'cannot be blank')) if send(login_field).blank?
    errors.add(password_field, I18n.t('error_messages.password_blank', default: 'cannot be blank')) if send("protected_#{password_field}").blank?
    return if errors.count > 0

    # check for unknown login
    self.attempted_record = search_for_record(find_by_login_method, send(login_field))
    if attempted_record.blank?
      generalize_credentials_error_messages? ?
        add_general_credentials_error :
        errors.add(login_field, I18n.t('error_messages.login_not_found', default: 'is not valid'))
      return
    end


    # custom check for invalid password
    ...

  end
end

【讨论】:

猜你喜欢
  • 2011-04-08
  • 2012-05-19
  • 1970-01-01
  • 2019-08-29
  • 1970-01-01
  • 2015-09-08
  • 2015-07-27
  • 2011-12-15
  • 2015-11-16
相关资源
最近更新 更多