Facebook Integration in Rails3 using ‘omniauth’ gem

Facebook Integration in Rails3 using ‘omniauth’ gem

< 1 Minute Read

feature11. Create a Facebook Application by using your ‘Facebook Developer account’
2. Get the ‘App ID’ and ‘App Secret’ of your created application

Add following gems to your Gemfile

  • gem ‘omniauth’
  • gem ‘omniauth-facebook’

Run ‘bundle install’

Create a middleware file named ‘omniauth.rb’ in ‘config/initializers’ path

Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'],
{:scope ≥ 'email, read_stream, read_friendlists, friends_likes, friends_status, offline_access, publish_stream'}

This will hold the facebook_app_key and facebook_secret_key with permissions

Set your custom callback url in routes

match '/auth/:provider/callback', :to ≥ 'sessions#create' match '/auth/failure', :to ≥ 'sessions#failure'

Write the following codes in the Sessions Controller

def create
auth_hash = request.env['omniauth.auth'] # Got The precise information from facebook
if  !auth_hash.nil?
@user = User.find_or_create_from_auth_hash(auth_hash)
self.current_user = @user
redirect_to '/'
def failure
flash[:notice] = "Sorry, but you didn't allow access to our app!"
redirect_to '/'

Write the following code in your view page. You can use a custom image for the Facebook like ‘facebook.png’.

<%=link_to image_tag('/images/facebook.png'),
"/auth/facebook" %>
Jayadev Das

Do what you do best in – that’s what I’ve always believed in and that’s what I preach. Over the past 25+ years (yup that’s my expertise ‘n’ experience in the Information Technology domain), I’ve been consulting to small, medium and large companies ‘bout Web Technologies, Mobile Future as well as on the good-and-bad of tech. Blogger, International Business Advisor, Web Technology Expert, Sales Guru, Startup Mentor, Insurance Sales Portal Expert & a Tennis Player. And top of all – a complete family man!

No Comments

Post A Comment

Exit pop up

Before You Go...!

Subscribe to Andolasoft Blog

Stay up to date with the latest techniques of web & mobile app development service tips and our SaaS and open source product news.
Holler Box