Using Omniauth to Authenticate your Users

Go Social or go home! This should be the mantra of any business in 2016. Online territory has already wormed its way into practically every business and there is no way out of digital bandwagon if you don’t want to be left behind. Social media has become second nature for most of us and no matter what you target market, if you are not meeting your audience where they hang out, your business is at a huge risk of falling behind. One of the best way to integrate social into your business is by allowing users to log in to your business through social channels.
Omniauth is one such great gem which allows you to easily integrate Social authentication providers, including Facebook, Google, Twitter and GitHub for your Ruby on Rails web application. Check out the steps below to learn on how to integrate these authentication providers into your app.

Let’s assume you have a rails application with devise gem or any login other system

Now open your Gemfile and reference the omniauth gem.

1 gem ‘omniauth’

Next, per usual, run the bundle install command to install the gem.

Creating a Provider on Social Media (Facebook)

To add a provider to Omniauth, you would require to sign up as a developer on the providers’ site. After signing up, you would be given two strings (kind of like username and a password), that you would need to pass on to Omniauth. However, if you’re using an OpenID provider, then all you need is the OpenID URL.

If you want to use Facebook authentication, visit and click ‘Create New App’

Add your Providers to the App.

Create a new file below config/initializers called omniauth.rb. You can configure your authentication providers through this file.

Add in the below codes into the file that you created earlier:

Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, YOUR_APP_ID, YOUR_APP_SECRET

This is all the configuration you need to get this going. The rest is taken care of by Omniauth, as we are going to find in the next step.

Open your app/controllers/sessions_controller.rb file and write the create method, like

def create
auth_hash = request.env[‘omniauth.auth’]

render :text => auth_hash.inspect

Creating the User Model

def create
auth_hash = request.env[‘omniauth.auth’]

@authorization = Authorization.find_by_provider_and_uid(auth_hash[“provider”], auth_hash[“uid”])
if @authorization
render :text => “Welcome back #{}! You have already signed up.”
user = :name => auth_hash[“user_info”][“name”], :email => auth_hash[“user_info”][“email”] :provider => auth_hash[“provider”], :uid => auth_hash[“uid”]

render :text => “Hi #{}! You’ve signed up.”

User.find_by_email(“[email protected]”).listings.approved.each do |u|
if u.listing_promotes.present?
list_with_promote <<
list_with_promote <<{|u| p }
list_without_promote <<
p “*******”



Subscribe For Latest Updates

Related Posts

Leave a Comment

Your email address will not be published. Required fields are marked *