簡體   English   中英

使用Devise和Omniauth編輯用戶

[英]Editing Users With Devise and Omniauth

我正在通過Railscast實施Devise和OmniAuth(以及Devise 文檔 ) - 目前,我有一個網站,訪問者可以使用他們的Facebook帳戶或填寫表格進行注冊。

但是,當通過OmniAuth注冊的用戶嘗試編輯他們的個人資料時,我遇到了麻煩。 當用戶提交對其個人資料的更改時,Devise會查找用戶的當前密碼,但是使用facebook登錄的用戶不知道他們的密碼(他們在用戶模型中自動設置):

  def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
    user = User.where(:provider => auth.provider, :uid => auth.uid).first
    unless user
      user = User.create(first_name:auth.extra.raw_info.first_name,
                         last_name:auth.extra.raw_info.last_name,
                           provider:auth.provider,
                           uid:auth.uid,
                           email:auth.info.email,
                           password:Devise.friendly_token[0,20]
                           )
    end
    user
  end

當用戶編輯他的信息時,如果他通過OmniAuth設置了他的帳戶,則該應用程序不應要求密碼確認。 教程建議方便的password_required? 方法將幫助我實現這一結果。 具體來說,將此方法添加到用戶模型意味着它應該僅在用戶未通過OmniAuth注冊時返回true(在這種情況下提供者屬性將為nil):

def password_required?
  super && provider.blank?
end

因此,一段代碼如:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
  <%= devise_error_messages! %>
    <%= render :partial => "essential_user_info_inputs", :locals => { :f => f } %>
    <%= render :partial => "inessential_user_info_inputs", :locals => { :f => f } %>
    <% if f.object.password_required? %> 
        <%= render :partial => "password_inputs", :locals => { :f => f } %>
        <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
        <%= f.password_field :current_password %>
    <% end %>
  <%= f.submit "Update" %>
<% end %>

理論上只在需要時顯示密碼輸入。 它還表明Devise內置了邏輯,稱OmniAuth用戶不需要使用密碼來編輯他們的帳戶。 我不知道這是否屬實,但是這種教程使它看起來像那樣。 但是當OmniAuth用戶嘗試編輯他的帳戶時,我會收到“當前密碼不能為空”。 非OmniAuth用戶也是如此(這是有道理的,因為密碼字段也不會出現在這些用戶的編輯頁面上)。

有些戳了一下確認了password_required? 當用戶通過OmniAuth注冊並通過網站的常規用戶注冊時,方法返回false。 即使我將其更改為只運行超類方法,它也會返回false。

有關password_required方法發生了什么的任何想法? 我在任何地方都找不到任何關於它的東西,但我覺得這就是現在絆倒的東西。

更新:

這現在有效,但是沒有使用Railscast中概述的方法,它依賴於requires_password? 方法,一個我仍然一無所知的話題。 相反,我實現了此處概述的解決方案,如此處所示 所以我現在只要求密碼用代碼更新非OmniAuth帳戶:

class Users::RegistrationsController < Devise::RegistrationsController
def update
    @user = User.find(current_user.id)
    email_changed = @user.email != params[:user][:email]
    is_facebook_account = !@user.provider.blank?

    successfully_updated = if !is_facebook_account
      @user.update_with_password(params[:user])
    else
      @user.update_without_password(params[:user])
    end

    if successfully_updated
      # Sign in the user bypassing validation in case his password changed
      sign_in @user, :bypass => true
      redirect_to root_path
    else
      render "edit"
    end
  end
end

最簡單的方法是覆蓋RegistrationsController的update_resource方法。 這是建議他們自己實現的控制器:

  # By default we want to require a password checks on update.
  # You can overwrite this method in your own RegistrationsController.
  def update_resource(resource, params)
    resource.update_with_password(params)
  end

因此解決方案是在您自己的控制器中覆蓋此方法,如下所示:

class Users::RegistrationsController < Devise::RegistrationsController

  # 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

end

我在下面的鏈接中添加了一個更新,其中包括我對Devise / OmniAuth更改用戶配置文件/密碼問題的解決方案,並收集了一些有用的鏈接:

stackoverflow - 允許用戶編輯帳戶而不保存設計中的密碼

我看到這個在某處使用過。


def update
    params[:user].delete(:current_password)
    params[:user].delete(:password)
    params[:user].delete(:password_confirmation)
    if current_user.update_without_password(params[:user])
      redirect_to somewhere_wicked_path, notice => "You rock"
    else
      render 'edit', :alert => 'you roll'
    end
end

在控制器的更新方法中使用類似的東西。 很確定該方法也在Devise中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM