Showing posts with label social sites login code. Show all posts
Showing posts with label social sites login code. Show all posts

Tuesday, August 30, 2016

Login with facebook

Step 1:

Add gem into gemfile
gem 'omniauth-facebook'

Step 2:

Facebook App setup
https://developers.facebook.com/

Step 3:

run command on terminal
bundle install
Step 4:
Need to add facebook app id and facebook secret key

config/initializers/devise.rb
config.omniauth :facebook, 'facebook app id', 'facebook app secret',{:scope => 'email'}


Step 5:

we will need to create a User model to store our information that Facebook returns. Run the commands below to create the user model.
rails g model user provider uid name oauth_token oauth_expires_at:datetime
rake db:migrate

Step 6:
create omniauth_callbacks_controller

devise/omniauth_callbacks_controller.rb
class Devise::OmniauthCallbacksController < ApplicationController
  skip_before_filter :authenticate_user!
  def facebook
    begin
      @user = User.from_facebook_omniauth(request.env['omniauth.auth'])
      session[:user_id] = @user.id
      sign_in @user
      flash[:success] = "Welcome, #{@user.name}!"
    rescue
      flash[:warning] = "There was an error while trying to authenticate you..."
    end
    redirect_to users_path
  end
  def failure
    redirect_to root_path
  end
end

Step 7:

  config/routes.rb
devise_for :users, :controllers => {:omniauth_callbacks => "devise/omniauth_callbacks",registrations:"devise/registrations"}
Step 8:

Write a method in user model
app/model/user.rb
 def self.from_facebook_omniauth(auth)
    email = auth.info.email.present? ? auth.info.email : "#{auth.uid}@facebook.com"
     user_exist = User.find_by_email(email)
     if user_exist.nil?
       where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
         user.provider = auth.provider
         user.uid = auth.uid
         user.email = email
         user.first_name = auth.info.name.split(' ')[0]
         user.last_name = auth.info.name.split(' ')[1]
         user.username = auth.info.name.split(' ')[0].downcase
         user.password = Devise.friendly_token[0,20]
       end
     else
       user_exist.update(provider: auth.provider, access_token: auth.credentials.token, uid: auth.uid, first_name: auth.info.name, last_name: nil)
     end
     user_exist = User.find_by_email(email)
   end



Monday, March 7, 2016

Rails Login with google+





Step 1:
    In gemfile added gem:-

    gem "omniauth-google-oauth2"

Step 2:
       Go to 'https://console.developers.google.com'
Step 3:
    config/initializers/devise.rb:-

    config.omniauth :google_oauth2, ['client_id', 'secret_key']

Step 4:
create omniauth_callbacks_controller

devise/omniauth_callbacks_controller.rb

class Devise::OmniauthCallbacksController < ApplicationController

def google_oauth2
begin
   @user = User.find_for_google_oauth2(request.env['omniauth.auth'], session[:provider])
   session[:user_id] = @user.id
   sign_in @user
   session[:provider] = nil
   flash[:success] = "Welcome, #{@user.name}!"
 rescue
   flash[:warning] = "There was an error while trying to authenticate you..."
 end
 redirect_to after_sign_in_path_for(@user)

end

      def failure
        redirect_to root_path
     end

end

Step 5:
   config/routes:

    devise_for :users, :controllers => {:omniauth_callbacks => "devise/omniauth_callbacks" }
      get 'auth/:provider/callback', to: 'sessions#create'
      get 'logout', to: 'sessions#destroy'
      get 'failure' => 'devise/omniauth_callbacks#google_oauth2'

      get 'google_oauth2' => 'devise/omniauth_callbacks#google_oauth2'

Step 6:
   app/model/user.rb

     def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
       data = access_token.info
       domain_info = data.email.split("@").last
       if domain_info == "ascratech.com" or domain_info == "ascratech.in"
         user = User.where(:email => data["email"], :provider => "google").first
         unless user
        devise_user = User.find_by_email(data["email"])
        unless devise_user
           user = User.new(:name => data["name"],
                           :provider => "google",
                           :email => data["email"],
                           :password => Devise.friendly_token[0,20]
                          )

           user.skip_confirmation!
            user.save
        else
          devise_user.provider = "google"
          devise_user.name = data["name"]
          devise_user.save
          user = devise_user
        end
      end
      return user
    end
  end