【问题标题】:Rails 3.2 Omniauth Devise - Add password - Skip current passwordRails 3.2 Omniauth 设计 - 添加密码 - 跳过当前密码
【发布时间】:2012-01-26 17:06:38
【问题描述】:

我正在使用 Omniauth 来允许用户通过 Facebook、Twitter 和 Google 登录(并创建一个帐户)。

但是,如果用户决定不再使用这些服务,而是继续使用他们的帐户,他们将需要添加密码。


如何让用户在不输入current password 的情况下为其帐户添加密码?*


当我让用户转到edit_user_registration_path(@user) 时,他们会看到下面的表格:

= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put, name: 'validation'}) do |f|
  = devise_error_messages!

  = f.label :email, class: 'block'
  = f.email_field :email, class: 'large'
  %span.infobar Keep this here unless you'd like to change your email

  = f.label :password, class: 'block'
  = f.password_field :password, class: 'large'
  %span.infobar Leave blank if you don't want to change it

  = f.label :password_confirmation, class: 'block'
  = f.password_field :password_confirmation, class: 'large'

  - if @user.password_required?
    = f.label :current_password, class: 'block'
    = f.password_field :current_password, class: 'large'
    %span.infobar We need your current password to confirm changes

  = f.submit "Update"

user.rb

def password_required?
  (authentications.empty? || !password.blank?) && super
end

如您所见,我有它,如果@user.password_required?,则显示当前密码字段。即使在尝试为帐户创建新密码时未显示此字段,表单也不会正确验证,因为需要当前密码。

【问题讨论】:

    标签: ruby-on-rails authentication devise omniauth


    【解决方案1】:

    我只是重写了 RegistrationsController#update 方法:

    class RegistrationsController < Devise::RegistrationsController
      def update
        # Override Devise to use update_attributes instead of update_with_password.
        # This is the only change we make.
        if resource.update_attributes(params[resource_name])
          set_flash_message :notice, :updated
          # Line below required if using Devise >= 1.2.0
          sign_in resource_name, resource, :bypass => true
          redirect_to after_update_path_for(resource)
        else
          clean_up_passwords(resource)
          render_with_scope :edit
        end
      end
    end
    

    来源:How To: Allow users to edit their account without providing a password

    【讨论】:

    • 这对我不起作用,因为提交了 :password 和 :password_confirmation 属性。如果密码的长度为
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多