首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Twilio短信忘记密码Rails

Twilio短信忘记密码Rails
EN

Stack Overflow用户
提问于 2017-10-21 20:50:14
回答 1查看 220关注 0票数 1

当用户在登录屏幕上单击“忘记我的密码”时,他们会被重定向到'/password-reset‘路由。现在,我试图了解如何正确的形式,输入您的电子邮件,以接收和短信从Twilio与代码。

代码语言:javascript
复制
<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是0,并且我不确定在表单开头url是否正确。对我来说,@user是空的,因为没有人登录,所以我不确定这应该是什么。

我的路线是

代码语言:javascript
复制
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

我的密码控制器看起来就像

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-21 21:21:47

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

PasswordsContoller

代码语言:javascript
复制
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

表单

代码语言:javascript
复制
<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方法通过电子邮件查找用户,并在找到用户时发送令牌。否则,它将显示一封未找到的电子邮件闪存消息,并将其重定向回遗忘的密码表单。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46867938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档