Devise - Omniauth – 如果用户通过 Facebook 登录,则隐藏密码字段

2024-01-10

将 Devise 与 Omniauth 结合使用,我成功地允许用户使用他们的 Facebook 帐户登录。在 Omniauth 的帮助下wiki https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password我还可以禁止在编辑帐户时输入密码。

我当前面临的问题是隐藏我的密码字段registrations#edit查看他们是否通过 Facebook 注册。作为这两个宝石的新手,我仍在努力调整。因此,当涉及到设置必要的辅助方法时,我有点不知所措。

按照 wiki 的指示,我的注册控制器如下所示:

class RegistrationsController < Devise::RegistrationsController
  before_filter :configure_permitted_parameters, if: :devise_controller?

  def update
    @user = User.find(current_user.id)

    successfully_updated = if needs_password?(@user, params)
      @user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update))
    else
      # remove the virtual current_password attribute
      # update_without_password doesn't know how to ignore it
      params[:user].delete(:current_password)
      @user.update_without_password(devise_parameter_sanitizer.sanitize(:account_update))
    end

    if successfully_updated
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case their password changed
      sign_in @user, :bypass => true
      redirect_to after_update_path_for(@user)
    else
      render "edit"
    end
  end

  private

  # check if we need password to update user data
  # ie if password or email was changed
  # extend this as needed
  def needs_password?(user, params)
    user.email != params[:user][:email] ||
      params[:user][:password].present? ||
      params[:user][:password_confirmation].present?
  end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) do |u|
      u.permit(:first_name, :last_name, :email, :password, :password_confirmation)
    end
    devise_parameter_sanitizer.for(:account_update) do |u|
      u.permit(:first_name, :last_name, :email, :password, :password_confirmation)
    end
  end
end

这是我的registrations#edit view:

<% provide :title, 'Edit Profile' %>

<h2>Edit <%= resource_name.to_s.humanize %></h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :first_name %><br />
  <%= f.text_field :first_name %></div>

  <div><%= f.label :last_name %><br />
  <%= f.text_field :last_name %></div>

  <div><%= f.label :email %><br />
  <%= f.email_field :email %></div>

  <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
    <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
  <% end %>

  <div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
    <%= f.password_field :password, autocomplete: "off" %></div>

  <div><%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %></div>

  <div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
    <%= f.password_field :current_password, autocomplete: "off" %></div>

  <div><%= f.submit "Update" %></div>
<% end %>

UPDATE

经过一番广泛的研究,我找到了一个question https://stackoverflow.com/questions/23325518/how-to-check-if-user-is-signed-in-with-omniauth这不恰当地回答了我正在寻找的内容hiding输入字段(问题中倒数第二条评论)。唯一的问题是,由于某种原因,它会触发密码验证错误,尽管它不存在......

   <div>
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name %>
  </div>

  <div>
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %>
  </div>

  <% if user_signed_in? && !current_user.provider == 'facebook' %>
    <div>
      <%= f.label :email %><br />
      <%= f.email_field :email %>
    </div>

    <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
      <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
    <% end %>

    <div>
      <%= f.label :password %><br />
      <%= f.password_field :password %>
    </div>

    <div>
      <%= f.label :password_confirmation %><br />
      <%= f.password_field :password_confirmation %>
    </div>

    <div>
      <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
      <%= f.password_field :current_password %>
    </div>

在搜索了该网站之后,我终于找到了如何让它发挥作用。

我的首要任务是使用注册控制器的第一个代码示例设计维基 https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password

  def update
    # For Rails 4
    account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
    # For Rails 3
    # account_update_params = params[:user]

    # required for settings form to submit when password is left blank
    if account_update_params[:password].blank?
      account_update_params.delete("password")
      account_update_params.delete("password_confirmation")
    end

    @user = User.find(current_user.id)
    if @user.update_attributes(account_update_params)
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case their password changed
      sign_in @user, :bypass => true
      redirect_to after_update_path_for(@user)
    else
      render "edit"
    end
  end

接下来是在上面的代码上方添加一个方法来覆盖对用户当前密码的验证。特别感谢 Lauri Laine 在这篇文章中的回答:使用 Devise 和 Omniauth 编辑用户 https://stackoverflow.com/questions/13436232/editing-users-with-devise-and-omniauth

  # Overwrite update_resource to let users to update their user without giving their password
  def update_resource(resource, params)
    if current_user.provider == "facebook"
      params.delete("current_password")
      resource.update_without_password(params)
    else
      resource.update_with_password(params)
    end
  end

最后我必须添加:current_password作为我的用户模型的虚拟属性,一切正常!

attr_accessor :current_password

希望这可以帮助任何已经/将要遇到此问题的人。我知道这让我困惑了一分钟,但很高兴我能够弄清楚这一切。很快将与omniauth 的twitter gem 一起测试这一点。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Devise - Omniauth – 如果用户通过 Facebook 登录,则隐藏密码字段 的相关文章

  • 如何在控制器内部使用 auto_link

    在我的控制器中 我需要构建一个 JSON 对象 如何在控制器内使用 auto link 现在它错误 NoMethodError undefined method mail to for
  • 同一模型之间的两个 has_many 链接

    I have users其中有products通过 habtm 链接 该链接正在运行 我想添加一个链接user模型和product模型 以跟踪creator该产品的 当然 谁并不总是拥有该产品 但是当我写在我的user and produc
  • 强参数不起作用

    使用 Ruby 1 9 3 Rails 3 2 13 Strong parameters 0 2 1 我遵循了教程和railscasts中的每一个指示 但我无法让strong parameters工作 这应该是非常简单的事情 但我看不出错误
  • Ruby on Rails 中的三重连接

    我对 Ruby on Rails 中的关联有疑问 应用程序中有项目 用户 角色和组 项目属于一个有用户的组 一个用户可以属于多个不同的组 但只能在该组中拥有一个特定的角色 例如 在一个组中 用户是项目所有者 但在另一个组中 他是作家 使用
  • 如何覆盖 Ruby Ranges 的 .. 和 ... 运算符以接受 Float::INFINITY?

    我想覆盖 and Ruby 中的运算符Range 原因是 我正在处理数据库中的无限日期范围 如果你拉一个infinty从 Postgres 中取出日期时间 你会得到一个Float INFINITY在红宝石中 问题是 我无法使用Float I
  • 如何阻止与 RSpec 和 Capybara 的外部连接?

    在我的 Rails 项目中 我想编写非理想条件的测试 例如缺乏互联网连接或超时 例如 我正在使用 gem 来联系 API 并且希望确保在我的应用程序和外部 API 之间存在连接问题时能够正确处理错误 我已经可以通过用录像机制作固定装置并从
  • 我的整个 Rails 应用程序仅从一个文件获取 css 为什么

    我为两个控制器添加了不同的 css 文件 但我的整个应用程序仅从product css 我为两个控制器添加了不同的 css 文件 但我的整个应用程序仅从product css我为两个控制器添加了不同的 css 文件 但我的整个应用程序仅从p
  • Firebase Auth - 最近登录多长时间

    我有一个个人资料选项卡 用户可以在其中按编辑并编辑他们的个人资料 我只想在必要时才需要他们的密码 所以想知道用户登录的时间是多少毫秒 这使得它不是最近登录 其中firebase会抛出错误 auth requires recent login
  • 如何以 Rails 方式处理 JavaScript 事件(例如“link_to :remote”)?

    我正在使用 Ruby on Rails 4 我想以 Rails 方式处理 JavaScript 事件 也就是说 例如 假设我有以下内容 link to destroy article path article method gt delet
  • Rails 4 i18n,如何转换子域用于区域设置的路由

    我正在使用子域来确定 Rails 4 网站中的区域设置 我完全按照我想要的方式使用区域设置切换器 但现在我需要翻译路线 并且我不确定继续的最佳方法 我看过https github com kwi i18n routing https git
  • ApplicationController 的未定义方法“helper_method”,Rails 5

    我正在尝试使用doorkeeper 将oAuth2 0 集成到我的仅rails api 应用程序中 但我不断收到此错误 ApplicationController 的未定义方法 helper method 但无法找到解决该问题的明确解决方案
  • 如何在 Rails 3.2.1 版本中注释 Rails 模型

    我正在尝试遵循一些在线教程来在 Rails 中注释我的模型 然而 似乎所有教程都在谈论过时的注释版本或不正确的安装 这真是一团糟 到目前为止我已经尝试过以下方法 1 在 Gemfile 中添加此内容 gem annotate 2 4 0 2
  • Facebook API Javascript JSON 响应

    function getUser FB api me function response console log Response is response alert Your name is response first name ale
  • FB.ui Facebook 分享不适用于帖子?

    当我发起行动时 FB ui method share this works fine href https www facebook com this works fine href https www facebook com 67850
  • 从 Ruby 中的 DateTime 变量获取时间

    我在 ruby 中工作 我有一个包含数据库中今天的日期时间的对象 我只想要时间截断数据 我怎样才能得到那个 Try 日期时间 strftime http www ruby doc org stdlib 1 9 3 libdoc date r
  • 我在 Facebook 上发现的这个奇怪的脚本是什么?

    这并不是一个帮助我自己编程的问题 但我在 Facebook 上发现了这个页面 其中有一个很酷的幻觉 并且一个页面上写着 要看到真正的幻觉 请将此代码复制并粘贴到您的地址栏中 并且有一个脚本 免责声明 请勿运行以下代码 javascript
  • Rails:CSRF 令牌不工作但已设置

    我在 Heroku 上有我的 Rails 3 应用程序 当我发送银行信息时 我得到 WARNING Can t verify CSRF token authenticity但我的 CSRF 令牌已设置 https gist github c
  • 为什么“捆绑”会在我的开发机器上安装生产 gem?

    Gemfile 说 gem sqlite3 groups gt development test gem mysql2 group gt production 然而当我打字时bundle install在我的开发机器上安装了所有 gem 我
  • vs2008 c#:Facebook.rest.api如何使用它来获取好友列表?

    如何在此基础上取得进一步的进步 获取好友列表的下一步是什么 string APIKey ConfigurationManager AppSettings API Key string APISecret ConfigurationManag
  • Ruby on Rails REST 设计问题 - 在账户之间转账

    我有一个 Account 类 想要实现转账屏幕以允许用户在 2 个账户之间转账 我将如何实现这种 RESTful 方式 我有标准帐户和休息操作 那很好 但我该如何实现转移呢 通常我只会向帐户控制器和相应的视图添加一个名为 transfer

随机推荐