Introduction As my coding partner and I are working on our final Rails project, I thought it was a great time to share how we integrated authentication to our blog app. One way we know to authenticate users is by the use of ‘sessions and cookies’ which is great, but if we wanted our users to sign in with their existing facebook credentials, a third-party service like is the coolest. ‘omniauth-facebook’ ‘omniauth-facebook’ What is devise and omniauth-facebook is a ruby on rails gem which handles all user authentication features in your rails application in a very flexible manner. Devise on the other hand enables user authentication (login/signup) using their existing facebook account. Omniauth-facebook In general the Omniauth-provider is a service which enables user authentication through 3rd-party services such as facebook, twitter, google, and so on. Setting up devise and omniauth-facebook Build a basic rails application rails new my-rails-app — =postgresql cd my-rails-app rails db:create rails db:migrate #create rails project database #navigate to project directory #create database run run Devise gem installation and setup rails g controller Pages home - root root : ‘pages#home’ #generate the pages controller to handle the app home page #open config/routes.rb set route to #add devise gem to gemfile #my-rails-app/Gemfile gem ‘devise’ bundle install rails g devise:install rails g devise:views rails g devise rails db:migrate run #set alerts and notices in application.html.erb run run User run Omniauth-facebook setup Go to and create facebook developer account 1. https://developers.facebook.com/ add ‘omniauth-facebook’ gem to your gemfile 2. #my-rails-app/Gemfile ‘omniauth-facebook’ gem 3. Update the User Table with the params needed rails g migration AddOmniauthToUsers provider: uid: : image: rails db:migrate run string string name string text run 4. Update initializer # config/initializers/devise.rb config.omniauth , “App ID”, “App Secret”, “ / :facebook callback_url: http: /localhost:3000/auth /facebook/callback " Update the model 5. # app/models/user.rb devise , => [ ] verify your schema the additional fields/columns :omniauthable :omniauth_providers :facebook for vi-Add a Link to Facebook app/views/pages/home.html.erb <% current_user %> <%= link_to “Sign with Facebook”, user_facebook_omniauth_authorize_path %> <% %> <%= link_to “Logout”, destroy_user_session_path, %> <% %> unless in else method: :delete end vii-Update Routes # config/routes.rb devise_for , => { => “users/omniauth_callbacks” } :users :controllers :omniauth_callbacks Create a users directory 6. /controllers/users mkdir app Create a users controller with the following omniauth methods 7. # app/controller/users/omniauth_callbacks_controller.rb @user = User.from_omniauth(request.env[“omniauth.auth”]) @user.persisted? sign_in_and_redirect @user, => set_flash_message( , , => “Facebook”) is_navigational_format? session[“devise.facebook_data”] = request.env[“omniauth.auth”] redirect_to new_user_registration_url redirect_to root_path < Devise::OmniauthCallbacksController class Users::OmniauthCallbacksController def facebook if :event :authentication :notice :success :kind if else end end def failure end end Add custom methods to the User model 8. # app/models/user.rb .tap data = session[“devise.facebook_data”] && session[“devise.facebook_data”][“extra”][“raw_info”] user.email = data[“email”] user.email.blank? where( auth.provider, auth.uid).first_or_create user.email = auth.info.email user.password = Devise.friendly_token[ , ] user.name = auth.info.name user.image = auth.info.image . def self new_with_session (params, session) super do |user| if if end end end . def self from_omniauth (auth) provider: uid: do |user| 0 20 # assuming the user model has a name # assuming the user model has an image end end REFERENCES https://github.com/rails-camp/facebook-omniauth-demo https://github.com/plataformatec/devise