ruby-on-rails - 在没有电子邮件的情况下重置设计密码 - Rails

标签 ruby-on-rails devise

所以我有一个应用程序,用户使用他们的手机号码登录并通过文本/短信获取通知。这是一个移动应用程序。我通过 applicationmailer 通过发送电子邮件到“33333333@vtext.com”等来发送文本。

但是,我遇到了如何覆盖密码重置说明的问题。我希望通过文本发送消息(我没有他们的电子邮件地址),但我如何覆盖设计来做到这一点?我可以让用户输入他们的号码,然后进行查找(我将联系人路径作为字段存储在用户中,我在后端生成字符串,他们不必这样做)。

想法?

非常感谢!

最佳答案

你可以通过改变你的
passwords_controller :

  def create
    assign_resource
    if @resource
      @resource.send_reset_password_instructions_email_sms
      errors = @resource.errors
      errors.empty? ? head(:no_content) : render_create_error(errors)
    else
      head(:not_found)
    end
  end

  private

  def assign_resource
    @email = resource_params[:email]
    phone_number = resource_params[:phone_number]
    if @email
      @resource = find_resource(:email, @email)
    elsif phone_number
      @resource = find_resource(:phone_number, phone_number)
    end
  end

  def find_resource(field, value)
    # overrides devise. To allow reset with other fields
    resource_class.where(field => value).first
  end

  def resource_params
    params.permit(:email, :phone_number)
  end

然后在用户模型中包含这个新的关注点
module Concerns
  module RecoverableCustomized
    extend ActiveSupport::Concern

    def send_reset_password_instructions_email_sms
      raw_token = set_reset_password_token
      send_reset_password_instructions_by_email(raw_token) if email
      send_reset_password_instructions_by_sms(raw_token) if phone_number
    end

    private

    def send_reset_password_instructions_by_email(raw_token)
      send_reset_password_instructions_notification(raw_token)
    end

    def send_reset_password_instructions_by_sms(raw_token)
      TexterResetPasswordJob.perform_later(id, raw_token)
    end
  end
end

它基本上使用设计方法 sent_reset_password_instructions 的私有(private)方法使用添加您自己的短信逻辑。

关于ruby-on-rails - 在没有电子邮件的情况下重置设计密码 - Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16511634/

相关文章:

ruby-on-rails - Twitter Bootstrap 使用 will_paginate

ruby-on-rails - 设计:确认后运行代码

ruby-on-rails - 唯一约束问题偏执狂 gem

python - 什么是 Ruby 中的 Python 文档字符串?

ruby-on-rails - Rails Alpha2 国家/地区 到 真实国家/地区名称

ruby-on-rails - Rails 迁移不同的数据库

ruby-on-rails - 使用 Rspec、Devise 和 Factory Girl 测试用户模型

ruby-on-rails - Rails API、设计和移动应用程序。

ruby-on-rails - 如何使用设计在表单中嵌套属性而不出现 HashWithIn DifferentAccess 错误?

ruby-on-rails - 使用 AJAX 解析对 SetExpressCheckout 数字商品的响应(集成 PayPal 数字商品以进行快速结账)?