【问题标题】:Twilio SMS Forgotten Password RailsTwilio SMS 忘记密码 Rails
【发布时间】:2018-04-02 17:41:32
【问题描述】:

当用户点击登录屏幕上的“忘记我的密码”时,他们会被重定向到路由“/password-reset”。现在,我正在尝试了解如何正确输入您的电子邮件以接收来自 Twilio 的带有代码的 SMS 的表单。

<div class="form-group", style="width:50%;">
  <%= form_for @user, url: password_patch_path(current_user) do |f| %>
    <div class="form-group">
      <%= f.label :email %>
      <%= f.email_field :email, class: "form-control"  %>
    </div>
    <%= f.submit "Get Confirmation Code", class: "btn btn-default" %>
  <% end %>
</div>

我遇到的问题是 @user 为 nil,我不确定表单开头的 url 是否正确。 @user 为 nil 对我来说很有意义,因为没有人登录,所以我不确定应该是什么。

我的路线是

get '/password-reset', :to => 'passwords#edit', as: :password_reset
post '/password-reset', :to => 'passwords#reset', as: :password_edit
patch '/password-confirmation', :to => 'passwords#update', as: :password_patch

我的密码控制器看起来像

class PasswordsController < ApplicationController
 before_action :authenticated?, only: [:edit, :update]

def reset
 ConfirmationSender.send_confirmation_to(current_user)
 redirect_to new_confirmation_path
end

def edit
 @user = current_user
end

def update
  if passwords_not_empty? && passwords_equal?
    current_user.update(password_params)
    redirect_to users_dashboard_path(current_user.username), success: "Password Updated"
    session[:authenticated] = false
  else
   redirect_to password_edit_path(current_user.username), warning: "Error, please try again."
  end
end

private

  def password_params
    params.require(:user).permit(:password, :password_confirmation)
  end

  def passwords_not_empty?
    params[:user][:password].length > 0 && params[:user][:password_confirmation].length > 0
  end

  def passwords_equal?
    params[:user][:password] == params[:user][:password_confirmation]
  end

  def authenticated?
    render :file => "#{Rails.root}/public/404.html", :status => 404 unless session[:authenticated]
  end
end

【问题讨论】:

    标签: ruby-on-rails forms twilio two-step-verification


    【解决方案1】:

    您是对的,如果用户忘记了他/她的密码,则不会有current_user。我会重新设计如下:

    密码控制器

    class PasswordsController < ApplicationController
      before_action :authenticated?, only: [:update]
    
      def reset
        @user = User.find_by(email: params[:user][:email])
        if @user.present?
          ConfirmationSender.send_confirmation_to(@user)
          redirect_to new_confirmation_path
        else
          redirect_to password_reset_path, warning: "Email not found."
        end
      end
    
      def edit
        @user = User.new
      end
    
    ...
    
    end
    

    表格

    <div class="form-group", style="width:50%;">
      <%= form_for @user, url: password_edit_path do |f| %>
        <div class="form-group">
          <%= f.label :email %>
          <%= f.email_field :email, class: "form-control"  %>
        </div>
        <%= f.submit "Get Confirmation Code", class: "btn btn-default" %>
      <% end %>
    </div>
    

    新的edit 方法为表单提供了一个空白用户。新的reset 方法通过电子邮件查找用户并在找到用户时发送令牌。如果没有,它会显示电子邮件未找到闪现消息并重定向回忘记密码表单。

    这也使表单使用正确的路径来请求密码确认。

    【讨论】:

    • 谢谢汤姆!非常感谢您的快速帮助。
    • 没问题。不要害怕为您认为有帮助的答案投票。
    猜你喜欢
    • 2014-08-05
    • 2011-08-28
    • 1970-01-01
    • 2012-07-25
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 2013-05-17
    相关资源
    最近更新 更多