2
votes

i have searching all these days to implement OmniAuth using facebook by the use of devise. I have visited so many sites including the git wiki https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview. But i did not get the answer i was looking for. Also i have downloaded a book for devise for the same,but same in book too. i have briefly explain what all things i have did for implementing the omniauth. steps

  1. download and install devise gem from https://github.com/plataformatec/devise#getting-started

2.make the devise ready using overriding the Registrations controller and views

3.I have already visited the rails casts http://railscasts.com/episodes/360-facebook-authentication?view=asciicast but not explanatory

i need a step by step explanation of how to implement OmniAuth-facebook in rails .

I have used rails 4 with ruby 2.2.1 .

2
gem 'omniauth-facebook' works well for me - asiniy
Is better you explain us, what's your problem, or what you tried by code - soltex
i would like to get a step by step explanation for that, can you do it? - Anoob K Bava
This may be useful for you: (Part1) youtube.com/watch?v=11BInedaQSo, (Part2) youtube.com/watch?v=1yRvsI34Ysw. It worked for me. - soltex

2 Answers

4
votes
  1. Add gem 'omniauth' to your Gemnfile
  2. Add 2 more columns i.e 'uid' and 'provider' to our user model
  3. Add gem 'omniauth-facebook' to your Gemfile
  4. Create an application in Facebook to get the secret key. Next, you need to declare the provider in your (config/initializers/devise.rb) and require it:

    require "omniauth-facebook"

    config.omniauth :facebook, "APP_ID", "APP_SECRET"

  5. make your model (e.g. app/models/user.rb) omniauthable:

    devise :omniauthable

  6. Restart the server
  7. Now Devise will create the following url methods.

    user_omniauth_authorize_path(provider)

    user_omniauth_callback_path(provide)

  8. Use the below line of code in your view file wherever you want to provide the Facebook link to authorize for the users:

    <%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>

    When the user clicks on the above link, they will redirects to the Facebook login page, after entering their credentials it will again redirect the user back to our applications Callback method

  9. When the user clicks on the above link, they will redirects to the Facebook login page, after entering their credentials it will again redirect the user back to our applications Callback method:

    devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

  10. Now we we are going to add a new controller file inside our Rails controller directory "app/controllers/users/omniauth_callbacks_controller.rb" and put the following line code in your omniauth_callbacks_controller.rb file.

    class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
      def facebook
        # You need to implement the method below in your model (e.g. app/models/user.rb)
        @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user) 
        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"] = request.env["omniauth.auth"]
          redirect_to new_user_registration_url
        end
      end
    end
    
  11. Now we are going to implement the find_for_facebook_oauth method in our user model (e.g. app/models/user.rb) :

    def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
      user = User.where(:provider => auth.provider, :uid => auth.uid).first
      if user
        return user
      else
        registered_user = User.where(:email => auth.info.email).first
        if registered_user
          return registered_user
        else
          user = User.create(name:auth.extra.raw_info.name,
                                        provider:auth.provider,
                                        uid:auth.uid,
                                        email:auth.info.email,
                                        password:Devise.friendly_token[0,20]
                                      )
        end
      end
    end