Skip to main content

Facebook Connect Integration Using Devise and OmniAuth In Rails App.

Add the gems in your gem file

gem ‘devise’
gem 'omniauth'
gem 'omniauth-facebook'

Run the “bundle install” command to install the gem.

Define your root url like below

root :to => “home#index”

Now you need to run the generator command

rails generate devise:install

This generator will install all Devise configurations.Take a look at them.

After done with the above options,you are ready to add Devise to any of your models using the generator:

rails generate devise User

This generator creates a few interesting things: a model file, a migration and a devise_for route.

Go the user model “user.rb” and add the following line

devise : omniauthable

Run the migrate command to insert the User table in your database.

rake db:migrate

It’ll insert the Users table with some columns.

You need two more columns to store provider type and userid given from facebook

rails g migration AddProviderToUsers provider:string uid:string

Runt rake db:migrate to insert the columns in users table.

First of all you need to create an app in facebook to get “App-ID” and “App Secret”

Create an app and get the App id and secret key.

Now you need to declare the provider name and app id and key.Go to the file config/initializers/devise.rb and the following line

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

Go to your layout file and the following block

<% if user_signed_in? %>
Signed in as <%= %>. Not you?
<%= link_to "Sign out", destroy_user_session_path,:method => :delete %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or
<%= link_to "Sign in", new_user_session_path %>
<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
<% end %>

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

Before creating the call back method change your route like below

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

Create a new controller named as “omniauth_callbacks_controller.rb”.Add the following method in it.

class OmniauthCallbacksController < Devise::OmniauthCallbacksController  
def facebook    
     @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?
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url

Add the following block in your user model.

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
      registered_user = User.where(:email =>
      if registered_user
        return registered_user
        user = User.create(,
      end    end


Popular posts from this blog

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).?

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).) In rails?

1)change the gemfile in mysql version
 gem 'mysql2'

2)Change to mysql2 version
 gem 'mysql2','0.3.20'

Active Scaffold : Add radio buttons to your form ruby on rails ?

Active Scaffold : Add radio buttons to your form ruby on rails ?

The way to add radio buttons to your active scaffold create/update from is as shown below


class UsersController < ApplicationController
active_scaffold :user do |config|
config.label = “Users”
config.columns = [:name, :gender]
config.columns[:gender].form_ui = :radio
config.columns[:gender].options[:options] = [['Male', '1'], ['Female','2']]

Get user accessing ip address in rails?

Get user accessing ip address rails?

request.ip ->returns the ip, whether is is a local proxy ip (localhost address) or not.
*request.remote_ip -> is smarter and gets the ip address of the client outside of local proxies.
3)If you are using apache in front of a mongrel, then remote_ip will return the source address of the request, which in this case will be local host because the Apache web server is making the request, so instead put this in your controller:

@remote_ip = request.env["HTTP_X_FORWARDED_FOR"]