2

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 .

4
  • gem 'omniauth-facebook' works well for me Commented Sep 22, 2015 at 11:09
  • Is better you explain us, what's your problem, or what you tried by code Commented Sep 22, 2015 at 11:10
  • i would like to get a step by step explanation for that, can you do it? Commented Sep 22, 2015 at 11:12
  • This may be useful for you: (Part1) youtube.com/watch?v=11BInedaQSo, (Part2) youtube.com/watch?v=1yRvsI34Ysw. It worked for me. Commented Sep 22, 2015 at 13:10

2 Answers 2

4
  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
    
Sign up to request clarification or add additional context in comments.

4 Comments

thanks rahul for the update, but i would like to point out some errors, in step 10 the code is not modified well. and i have got an error name
on this line the error is shown, name:auth.extra.raw_info.name,
To add a sign out link use: <% if user_signed_in? %> <%= link_to('Logout', destroy_user_session_path, :method => :delete) %> <% end %>. Its seems that you are not having a name field in user model. Try adding one. That should fix the error.
when i click on sign out , it shows log out .but when i try again for sign in using facebook it automatically login using previous credentials also , i cant login using normal way. in server log it shows unpermited parameter " remember-me"
0

You can find information in devise wiki

https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.