I am creating an api in rails for mobile application. Now the user will login using facebook and i will recieve the access token post facebook authentication. I have to take this access token and check if the access token exists in applications database. If it does then allow user access, if not then check for email and update the access token and allow user access.
How do i accomplish this with omniauth-facebook and devise , as far as i can see omniauth-facebook does everything from ground up, can't find a way to provide only access token to omniauth-facebook.
Any kinda help would be great.
You don't need omniauth-facebook. This gem's purpose is only to obtain an access token (and query the user's informations). Here's how things happen in a standard setup (with Devise and omniauth-facebook):
(see Devise's documentation about Omniauth for more details)
Now, given that you already have an access token, and that fetching the user's informations from Facebook's API is very simple, you don't actually need omniauth-facebook.
All you need to do is to write an action to:
This code is based on the example given in 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
Notes:
sign_in_and_redirect
, set_flash_message
and is_navigational_format?
are helpers provided by DeviseUser.find_for_facebook_oauth
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With