【问题标题】:is there any account recovery gem for ruby on rails that uses SMS?是否有任何使用 SMS 的 ruby​​ on rails 帐户恢复宝石?
【发布时间】:2013-08-27 22:17:58
【问题描述】:

我想知道是否有任何用于 ruby​​ on rails 密码的帐户恢复 gem,允许应用程序向用户发送他的 sms 密码以在用户忘记密码时重置密码?谷歌搜索但没有看到任何东西,我想我会在这里问,以防我的谷歌搜索字符串写得不好。

ruby on rails account recovery via sms

【问题讨论】:

    标签: ruby ruby-on-rails-3 passwords sms


    【解决方案1】:

    我不知道有任何 gem,但这听起来并不难实现。正如 vgoff 所提到的,有很多 SMS 服务可供您使用。

    类似的东西(还没有测试过):

    class SMSReset < ActiveRecord::Base
      TOKEN_LENGTH = 4
      EXPIRY_TIME = 15.minutes
    
      belongs_to :user
    
      before_create :generate_token, :set_expiry
    
      def dispatch_sms!
        MySMSProvider.send_sms(to: user.mobile_number, body: "Your SMS token is: #{token}")
      end
    
      def has_not_expired?
        expires_at > Time.now
      end
    
      private
    
      def generate_token
        self[:token] = SecureRandom.hex[0..TOKEN_LENGTH - 1].downcase
      end
    
      def set_expiry
        self[:expires_at] = EXPIRY_TIME.from_now
      end
    end
    
    
    class PasswordResetController < ApplicationController
      def new
      end
    
      def create
        @user = User.where(email: params[:email]).first
    
        if @user
          sms_reset = @user.create_sms_reset!
          sms_reset.dispatch_sms!
          flash.now[:success] = "Please enter the code that was sent to your phone in the field below"
        else
          flash.now[:error] = "No user was found by that email address"
          render :new
        end
      end
    
      def validate_token
        sms_reset = SMSReset.where(user_id: params[:user_id], token: params[:token])
    
        if sms_reset.present? && sms_reset.has_not_expired?
          @user = sms_reset.user
          render :password_reset_form
        else
          flash.now[:error] = "Sorry, that code wasn't recognized"
          render :new
        end
      end
    end
    

    你会想要处理错误,还有改进的余地,但希望这个要点是有意义的。

    【讨论】:

    • 确实如此。我还不是 Rails 专家,所以需要做一些腿部工作,但这里的想法似乎很可靠。我正在与 Devise 和 Cancan 合作。我喜欢 MySMSProvider :) 有什么好的建议吗?我在想twilio.com
    • @Codejoy 根据我的经验,它们大多是可以互换的。您可以毫不费力地抽象出您正在使用的提供程序。使用 Twilio,看看它对你有什么影响。
    【解决方案2】:

    不是我直接知道的,但https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=sms 为 SMS 交互提供了一些宝石。

    这是我最先看的地方之一,也是直接在 github.com 上搜索的地方之一。 RubyForge 是寻找宝石的另一个很好的信息来源。

    https://rubyforge.org/search/?type_of_search=soft&words=sms&Search=Search

    【讨论】:

      猜你喜欢
      • 2012-05-08
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 2013-07-15
      • 1970-01-01
      相关资源
      最近更新 更多