Making OmniAuth, Devise and Koala work together

扶醉桌前 提交于 2019-12-02 18:37:51

You can accomplish this using something like Koala. When you authenticate the user, you can grab the access token. Assuming you've followed the Devise/Omniauth tutorial, you could do something like so:

  def self.find_for_facebook_oauth(response, signed_in_resource=nil)
    data = response['extra']['user_hash']
    access_token = response['credentials']['token']
    user = User.find_by_email(data["email"])
    # only log in confirmed users
    # that way users can't spoof accounts
    if user and user.confirmed?
      user.update_attribute('fb_access_token', access_token) 
      user
    end
  end

Once you have the access token, you could then do something like:

@graph = Koala::Facebook::API.new(@user.fb_access_token)
profile_image = @graph.get_picture("me")

In my app, I check to see if a user is logged in when the callback from Facebook comes. If they are, I assume the request was to link accounts. If they're not, I assume it's a login request.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!