0
用戶可以使用omniauth提供程序登錄到我的Rails應用程序,但我想僅爲具有密碼的用戶啓用某些功能。使用Omniauth和Devise爲用戶登錄到Rails應用程序創建密碼
當用戶使用omniauth憑證創建時,其密碼爲空。當我嘗試爲omniauth用戶創建密碼時,出現錯誤「當前密碼不能爲空」。
如何爲具有「nil」作爲其current_password的omniauth用戶創建密碼?
這是我目前有我的形式修改用戶的密碼:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<% if current_user.password != nil %>
<div class="formField"><%= f.label :current_password, :class=>"label label-info " %> <br />
<%= f.password_field :current_password %></div>
<% else %>
<%= f.hidden_field :current_password, :value => nil %>
<% end %>
<div class="formField"><%= f.label "New password", :class=>"label label-info" %> <br />
<%= f.password_field :password, :autocomplete => "off" %>
</div>
<div class="formField"><%= f.label "New password confirmation", :class=>"label label-info" %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="editAccountButtons">
<%= f.submit "Update", :class=>"btn btn-small btn-info submitButton" %>
</div>
<% end %>
registrations_controller.rb
def update
@user = User.find(current_user.id)
successfully_updated = if needs_password?(@user, params)
Rails.logger.debug('updating with password')
@user.update_with_password(params[:user])
else
# remove the virtual current_password attribute
if @user.provider.blank?
@user.update_without_password(params[:user])
else
@user.update_with_password(params[:user])
end
end
if successfully_updated
if params[:update_email]
set_flash_message :alert, :signed_up_but_unconfirmed
redirect_to after_update_path_for(@user)
else
set_flash_message :notice, :updated
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
end
else
redirect_to :back, alert: resource.errors.full_messages[0]
end
end
# check if we need password to update user data
def needs_password?(user,params)
!params[:profile] && !user.password.blank?
end