Step-1 Put gem into gemfile.
gem 'omniauth-facebook'
gem 'omniauth-twitter'
gem "omniauth-google-oauth2"
gem 'omniauth-twitter'
gem "omniauth-google-oauth2"
gem 'omniauth'
and after that run bundle command on your console.
Step-2 App->Config->initializer-> omniauth.rb.
copy and paste few line below given.
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, '10370967297316xxx' , '6703b75235b704e789aa9fbb649xxxx'
or set environment variable
or set environment variable
provider :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"]
provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
end
after you will change some thing in your config file you have to restart the server again.
Step-3 you have to added migration
rails g migration AddOmniautoToUsers
open migration file copy and paste this code
add_column :users, :provider, :string
add_column :users, :uid, :string
add_column :users, :name, :string
in console type command -> rake db:migrate
step -4 user model
def self.sign_in_from_omniauth(auth)
user = where(provider: auth['provider'], uid: auth['uid']).first_or_initialize
user.email = auth['info']['email']
user.name = auth['info']['name']
user.password = Devise.friendly_token[0,20]
user.confirmed_at = Time.now
user.save
user
end
step -5 User controller
def omniauth
auth=request.env["omniauth.auth"]
session[:omniauth] = auth.except('extra')
user = User.sign_in_from_omniauth(auth)
sign_in user
redirect_to root_url, notice: "SIGNED IN"
end
Step-6 in routes.rb
get 'auth/:provider/callback', to: "users#omniauth"
Step 7 - put this is link in view
<%= link_to "/auth/facebook" do %>Facebook<% end %>
<%= link_to 'Twitter', '/auth/twitter' %>
<%= link_to 'Google+', '/auth/google' %>
No comments:
Post a Comment