Tuesday, November 25, 2014

Integrating facebook using koala gem

In view:

<div id=”fb-root”></div>
<script src=”http://connect.facebook.net/en_US/all.js”&gt;
</script>
<script>
FB.init({
appId:’204597759582691′, cookie:true,
status:true, xfbml:true
});
</script>
<fb:login-button v=”2″ size=”xlarge” length=”long”
perms=”email” onlogin=’window.location = “/app/code”‘ >
Login with Facebook
</fb:login-button>
<div>
<a href=””>signin facebook</a>
</div>

In Controller:

class AppController < ApplicationController
def code
“set_facebook_client” #to check whether the user already login with the face book
oauth = Koala::Facebook::OAuth.new(204597759582691, “df14fdac2d0ebf6fbf684c68b2009ef8″)
#~ oauth = Koala::Facebook::OAuth.new(appid, “appsecretkey”)
@facebook_client ||= if @facebook_cookies = oauth.get_user_info_from_cookies(cookies)
Koala::Facebook::GraphAPI.new(@facebook_cookies[‘access_token’])
end
profile = @facebook_client.get_object(“me”)
end
end


In gemfile:
gem ‘koala’

Integrating Twitter Login With Rails Application

Follow the below steps to create Twitter application for your rails application:

1. Goto https://dev.twitter.com/
2. Click “Create an App” link.
3. Enter your app name, description, website and call back url.
For Website and call back url enter “http://127.0.0.1:3000/&#8221; for local host or your application url say “http://www.twitterapp.com&#8221;
5. You will get the Consumer key and Consumer secret.

Follow the below steps to integrate the Twitter login:


1. Include the below in your Gemfile.
    gem “omniauth-twitter”  
2. Execute the below command to install the gem
bundle
3. Create a new file called “omniauth.rb” in your initializers folder(app/config/initializers)
4. Inside the “omniauth.rb” copy the below lines
 Rails.application.config.middleware.use OmniAuth::Builder do
   provider :twitter, ‘YOUR APP CONSUMER KEY’, ‘YOUR APP CONSUMER SECRET’
 end
5. Paste the below lines to your routes file
  #calback url from twitter
       match ‘/auth/:provider/callback’ => ‘users#twitter_login’
6. Paste the below lines to your users controller
def twitter_login
 omniauth = request.env[‘omniauth.auth’]   # This contains all the details of the user say Email, Name, Age so that you can store it in your application db.
    redirect_to “Your redirect path after user logged in”
  end
7. Paste the below line to your view file where you want to display the Login with facebook link, Say in your users/new.html.erb
<a href=”/auth/twitter”>Login Via Twitter</a>
8. Restart the Server
9. Go to http://localhost:3000/users/new
10. Click the link  “Login Via Twitter” , page will be redirected to the Twitter site then redirected to your home page.

Integrating Facebook Login With Rails Application

Create Application on Facebook:

1. Goto https://developers.facebook.com/
2. Click “Apss” link.
3. Click “Create App”
4. Enter your app name “test”
5. You will get the App Id and App Secret
6. Click the link “Website with Facebook Login” and enter your site url say “http://localhost:3000/&#8221;
7. Click Save Changes.

Integrate Facebook Login to your application:

1. Include the below in your Gemfile.
gem “omniauth-facebook”
2. Execute the below command in terminal.
bundle
3. Create a new file called “omniauth.rb” in your initializers folder(app/config/initializers)
4. Inside the “omniauth.rb” copy the below lines
 Rails.application.config.middleware.use OmniAuth::Builder do
   provider :facebook, ‘YOUR APP KEY’, ‘YOUR APP SECRET’
 end
5. Paste the below lines to your routes file
    #calback url from facebook
      match ‘/auth/:provider/callback’ => ‘users#facebook_login’
6. Paste the below lines to your users controller
def facebook_login
      omniauth = request.env[‘omniauth.auth’]   # This contains all the details of the user say Email, Name, Age so that you can store it in your application db.
      redirect_to “Your redirect path after user logged in”
    end
7. Paste the below line to your view file where you want to display the Login with facebook link, Say in your users/new.html.erb
<a href=”/auth/facebook”>Login Via facebook</a>
8. Restart the Server
9. Go to http://localhost:3000/users/new

10. Click the link  “Login Via Facebook” , page will be redirected to the Facebook site then redirected to your home page.