【发布时间】:2018-08-01 13:10:22
【问题描述】:
参考资料:
Rails 5.0.1
omniauth 1.8.1
omniauth-facebook 5.0.0
我有用户使用电子邮件 + 密码登录,其他用户使用 facebook 帐户登录。
主要问题是让 facebook 的用户无需使用密码即可编辑自己的帐户,因为他们根本没有。 this post中的解决方案,所以我可以添加这个类:
def update
if current_user.provider == "facebook"
params.delete("current_password")
resource.update_without_password(user_params)
else
resource.update_with_password(user_params)
end
redirect_to root_path
end
所以我有这个:
routes.br
devise_for :users, controllers: { omniauth_callbacks: "omniauth_callbacks" }
在 omniauth_callbacks_controller.rb 我添加了这个 update 函数:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def update
if current_user.provider == "facebook"
params.delete("current_password")
resource.update_without_password(user_params)
else
resource.update_with_password(user_params)
end
redirect_to root_path
end
def facebook
user = User.find_for_facebook_oauth(request.env['omniauth.auth'])
if user.persisted?
sign_in user
redirect_to root_path
set_flash_message(:notice, :success, kind: 'Facebook') if is_navigational_format?
else
session['devise.facebook_data'] = request.env['omniauth.auth']
redirect_to new_user_registration_url
end
end
def user_params
params.require(:user).permit(:first_name, :last_name, :phone, :picture, :facebook_picture_url)
end
end
问题是该设备不读取此功能。因此,如果我们在里面添加一个 byebug,它就不会停在那里。
覆盖此更新功能的唯一方法是在下面添加此配置。
routes.br
devise_for :users, controllers: { registrations: 'registrations' }
registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def update
if current_user.provider == "facebook"
params.delete("current_password")
resource.update_without_password(user_params)
else
resource.update_with_password(user_params)
end
redirect_to root_path
end
def facebook
user = User.find_for_facebook_oauth(request.env['omniauth.auth'])
if user.persisted?
sign_in user
redirect_to root_path
set_flash_message(:notice, :success, kind: 'Facebook') if is_navigational_format?
else
session['devise.facebook_data'] = request.env['omniauth.auth']
redirect_to new_user_registration_url
end
end
def user_params
params.require(:user).permit(:first_name, :last_name, :phone, :picture, :facebook_picture_url)
end
end
第二种方法的唯一问题是,如果我尝试使用 facebook 登录,我会收到以下错误消息:
找不到操作“facebook” 设计::OmniauthCallbacksController
我的设计配置:
devise.rb
Devise.setup do |config|
config.omniauth :facebook, ENV["FB_ID"], ENV["FB_SECRET"],
scope: 'email',
info_fields: 'email, first_name, last_name',
image_size: 'large', # 50x50, guaranteed ratio
secure_image_url: true
...
end
【问题讨论】:
-
你的
routes.rb文件不应该像devise_for :users, controllers: { registrations: 'registrations' , omniauth_callbacks: 'omniauth_callbacks' }吗? -
是的,你是对的! @gasc 非常感谢!!!
标签: ruby-on-rails devise omniauth