在Rails中,没有电子邮件的情况下重置Devise密码

5
所以我有一个应用程序,用户使用他们的手机号码登录并通过短信接收通知。这是一个移动应用程序。我通过向“33333333@vtext.com”等发送电子邮件来通过应用程序发送短信。
然而,我在如何覆盖密码重置说明方面遇到了难题。我希望消息通过短信发送(我没有他们的电子邮件地址),但是如何覆盖设备来实现这一点呢?我可以让用户输入他们的号码,然后进行查找(我将联系路径存储为用户字段,我在后端生成字符串,他们不必这样做)。
有什么想法吗?
非常感谢!
1个回答

2
你可以通过更改你的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

这基本上使用 devise 方法 sent_reset_password_instructions 使用的私有方法,添加您自己的文本逻辑。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接