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 developers.facebook.com/apps 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
end
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
end
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 #{@authorization.user.name}! You have already signed up.”
else
user = User.new :name => auth_hash[“user_info”][“name”], :email => auth_hash[“user_info”][“email”]
user.authorizations.build :provider => auth_hash[“provider”], :uid => auth_hash[“uid”]
user.saverender :text => “Hi #{user.name}! You’ve signed up.”
end
endlist_with_promote=[]
list_without_promote=[]
User.find_by_email(“[email protected]”).listings.approved.each do |u|
if u.listing_promotes.present?
list_with_promote << u.id
list_with_promote << u.listing_promotes.map{|u| p u.promote.name }
else
list_without_promote << u.id
end
p “*******”
end
References;