Creation OmniAuth, Devise and Koala work together

I have an application in which I implement authentication using devise and omniauth. Ive got input in / outputs, etc., I found out that I wanted to know, this is the most efficient way to connect an end user to the api graphic element that has been initialized and ready to be used in my application.

For example: If on the profile page I wanted to do,

profile_image = current_user.fbgraph.get_picture("me") 

How could I accomplish this with the least number of API calls (I will use similar calls throughout the application)

+6
source share
1 answer

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

  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 can do something like:

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

In my application, I check if the user is registered with a callback from Facebook. If they exist, I assume that the request was associated with accounts. If this is not the case, I assume this is the login.

+9
source

Source: https://habr.com/ru/post/891710/


All Articles