Here is an example to demonstrate how to integrate Facebook connect with devise authentication from scratch in a rails app.
Step:1
Add the gems in your gem file
gem ‘devise’
gem 'omniauth'
gem 'omniauth-facebook'
Run the “bundle install” command to install the gems.
Step:2
Define your root url like below
root :to => “home#index”
Step:3
Now you need to run the generator command
rails generate devise:install
This generator will install all Devise configurations.Take a look at them.
Step:4
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.
Step:5
Go the user model “user.rb” and add the following line
devise : omniauthable
Step:6
Run the migrate command to insert the User table in your database.
rake db:migrate
It’ll insert the Users table with some columns.
Step:6
You need two more columns to store provider type and userid given from facebook
rails g migration AddProviderToUsers provider:string uid:string
Run rake db:migrate to insert the columns in users table.
Step:7
First of all you need to create an app in facebook to get “App-ID” and “App Secret”
https://developers.facebook.com/
Create an app and get the App id and secret key.
Step:8
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"
Step:9
Go to your layout file and the following block
<% if user_signed_in? %>
Signed in as <%= current_user.name %>. 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 .
Step:10
Before creating the call back method change your route like below
devise_for :users, :controllers => { : omniauth_callbacks => "omniauth_callbacks" }
Step:11
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?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
Step:12
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
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
Now we are done with facebook integration with devise.
Step:1
Add the gems in your gem file
gem ‘devise’
gem 'omniauth'
gem 'omniauth-facebook'
Run the “bundle install” command to install the gems.
Step:2
Define your root url like below
root :to => “home#index”
Step:3
Now you need to run the generator command
rails generate devise:install
This generator will install all Devise configurations.Take a look at them.
Step:4
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.
Step:5
Go the user model “user.rb” and add the following line
devise : omniauthable
Step:6
Run the migrate command to insert the User table in your database.
rake db:migrate
It’ll insert the Users table with some columns.
Step:6
You need two more columns to store provider type and userid given from facebook
rails g migration AddProviderToUsers provider:string uid:string
Run rake db:migrate to insert the columns in users table.
Step:7
First of all you need to create an app in facebook to get “App-ID” and “App Secret”
https://developers.facebook.com/
Create an app and get the App id and secret key.
Step:8
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"
Step:9
Go to your layout file and the following block
<% if user_signed_in? %>
Signed in as <%= current_user.name %>. 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 .
Step:10
Before creating the call back method change your route like below
devise_for :users, :controllers => { : omniauth_callbacks => "omniauth_callbacks" }
Step:11
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?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
Step:12
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
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
Now we are done with facebook integration with devise.