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