Simple OAuth Authentication by Twitter Facebook and Google
== This is simple Omniauth Example without devise
=====Copy/Paste this gem in your gem file
=====This gem is working on Rails 3.2.6 version with ruby 1.9.3p545
gem ‘jquery-rails’
gem ‘omniauth-facebook’, ‘1.4.0’
gem ‘omniauth-twitter’, ‘~> 1.0.0’
gem ‘omniauth-google-oauth2’
gem ‘json_pure’
gem ‘json’
====Then add this code on sessions_controller.rb (if you have session_controller.rb file is exist in controller folder if not make a file)
class SessionsController < ApplicationController
def create
#raise request.env[“omniauth.auth”].to_yaml
auth = request.env[“omniauth.auth”]
user = User.find_by_provider_and_uid(auth[“provider”], auth[“uid”]) || User.create_with_omniauth(auth)
session[:user_id] = user.id
redirect_to root_url, :notice => “Singned In !”
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => “Singned Out !”
end
end
====Now next step to add this code in application_controller.rb in your controller folder
helper_method :current_user
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
====Now add this code in User model in your model folder
class User < ActiveRecord::Base
def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth[“provider”]
user.uid = auth[“uid”]
user.name = auth[“info”][“name”]
end
end
end
====Now this is your last step create a omniauth.rb file in you config/initializers folder
=====Now add this code in omniauth.rb file
equire “omniauth-google-oauth2”
OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ‘KEY’, ‘SCERET’ #goto facebook develper page and create a app and paste KEY and SCERET
provider :twitter, ‘KEY’, ‘SCERET’ # Goto dev.twitter and create a app then paste KEY and SCERET
provider :google_oauth2, ‘xxxxxxxxxxxxx.apps.googleusercontent.com’, ‘SCERET’, { access_type: “offline”, approval_prompt: “” }
end