Tuesday, November 25, 2014

Integrating Facebook Login With Rails Application

Create Application on Facebook:

1. Goto https://developers.facebook.com/
2. Click “Apss” link.
3. Click “Create App”
4. Enter your app name “test”
5. You will get the App Id and App Secret
6. Click the link “Website with Facebook Login” and enter your site url say “http://localhost:3000/”
7. Click Save Changes.

Integrate Facebook Login to your application:

1. Include the below in your Gemfile.
gem “omniauth-facebook”
2. Execute the below command in terminal.
bundle
3. Create a new file called “omniauth.rb” in your initializers folder(app/config/initializers)
4. Inside the “omniauth.rb” copy the below lines
 Rails.application.config.middleware.use OmniAuth::Builder do
   provider :facebook, ‘YOUR APP KEY’, ‘YOUR APP SECRET’
 end
5. Paste the below lines to your routes file
    #calback url from facebook
      match ‘/auth/:provider/callback’ => ‘users#facebook_login’
6. Paste the below lines to your users controller
def facebook_login
      omniauth = request.env[‘omniauth.auth’]   # This contains all the details of the user say Email, Name, Age so that you can store it in your application db.
      redirect_to “Your redirect path after user logged in”
    end
7. Paste the below line to your view file where you want to display the Login with facebook link, Say in your users/new.html.erb
<a href=”/auth/facebook”>Login Via facebook</a>
8. Restart the Server
9. Go to http://localhost:3000/users/new

10. Click the link  “Login Via Facebook” , page will be redirected to the Facebook site then redirected to your home page.

No comments:

Post a Comment