你不需要omniauth-facebook。这个宝石的目的只是为了获取访问令牌(并查询用户的信息)。 这里的事情是如何发生的标准设置(与设计和omniauth Facebook的):
- Omniauth获得它在请求的环境的访问令牌
- Omniauth通过通过Facebook的API和商店获取用户的信息
- Omniauth调用应用程序中的一个自定义的回调方法,你需要写
- 这个回调方法检索或在数据库中创建用户,然后使用由设计提供了几个助手登录用户。
(见Devise's documentation about Omniauth了解详细信息)
。现在,因为你已经有一个访问令牌,以及从Facebook的API获取用户的信息很简单,你实际上并不需要omniauth Facebook的。
所有你需要做的是写一个动作:
- 获得访问令牌(作为参数)
- 获取用户信息(一个Graph API请求/me)
- 发现或创建数据库用户(使用用户的Facebook ID)
- 签署用户使用设计的助手
此代码基于Devise's documentation中给出的示例。
class Users::FacebookCallbacksController
def facebook
# You should test if params[:access_token] is present
# and if this request fails
facebook_data = HTTParty.get("https://graph.facebook.com/me", query: {
access_token: params[:access_token]
}).parsed_response
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_facebook_oauth(facebook_data)
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = facebook_data
redirect_to new_user_registration_url
end
end
end
注: