Skip to content

Instantly share code, notes, and snippets.

@rdetert
Created March 1, 2011 06:28
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rdetert/848721 to your computer and use it in GitHub Desktop.
Save rdetert/848721 to your computer and use it in GitHub Desktop.
How to logout completely from Facebook using Ruby on Rails and Devise + Omniauth. I'm just modifying the Omniauth Railscast http://railscasts.com/episodes/236-omniauth-part-2
<div id="user_nav">
<% if user_signed_in? %>
<img src="<%= user_avatar %>" id="main_avatar"> Signed in as <%= current_user.email %>.<br />
Not you?
<% if session[:fb_token].nil? %>
<%= link_to "Sign out", destroy_user_session_path %>
<% else %>
<%= link_to "Sign out", facebook_logout_path %>
<% end %>
<% else %>
<%= link_to "Sign In", new_user_session_path %>
<%= link_to "Sign Up", new_user_registration_path %>
<% end %>
</div>
def facebook_logout
split_token = session[:fb_token].split("|")
fb_api_key = split_token[0]
fb_session_key = split_token[1]
redirect_to "http://www.facebook.com/logout.php?api_key=#{fb_api_key}&session_key=#{fb_session_key}&confirm=1&next=#{destroy_user_session_url}";
end
class Users::AuthenticationsController < BaseController
layout false
def create
omniauth = request.env["omniauth.auth"]
session[:fb_token] = omniauth["credentials"]["token"] if omniauth['provider'] == 'facebook'
# ... Same as Railscast ... #
end
def failure
render :text => "Login Failure!"
end
end
match '/auth/facebook/logout' => 'application#facebook_logout', :as => :facebook_logout
match '/auth/:provider/callback' => 'users/authentications#create'
match '/auth/failure' => 'users/authentications#failure'
devise_for :users,
:controllers => {:registrations => 'users/registrations', :sessions => 'users/sessions'}
class Users::SessionsController < Devise::SessionsController
def destroy
super
session[:fb_token] = nil
end
end
@pduey
Copy link

pduey commented Jun 21, 2011

I believe for "my_key" above, you can find it with, for the facebook example, Devise.omniauth_configs[:facebook].args[0].

@rdetert
Copy link
Author

rdetert commented Jun 21, 2011

Yes, you are right, thanks for that. The access token actually consists of 3 parts: app_id "|" session_key "|" digest
http://www.quora.com/Do-the-OAuth2-access-tokens-in-the-new-Facebook-Graph-API-expire

I've updated the code accordingly.

@carloshlopez
Copy link

Thanks, worked fine so far.

@Rameshv
Copy link

Rameshv commented Dec 8, 2011

It no longer works... Now fb returns omniauth["credentials"]["token"] as a single string...

access_key is changed to access_token. And now you have to pass full access-token instead the split

session_key is same. It your Application secret key.

https://www.facebook.com/logout.php?access_token=xx&session_key=yy

Cheers

@anitabharambe
Copy link

Hi Rameshv,
I have tried the above solution, but it didn't worked for me.
I am passing the access_token stored in session i.e omniauth["credentials"]["token"] and the app secret key, but still it does not logout from my facebook account.
I am using rails 2.3.11, ruby 1.8.7, omniauth 0.1.6
Any help is appreciated.

Thanks
Anita Bharambe

@maurogeorge
Copy link

I got success doing a GET to this URL

https://www.facebook.com/logout.php?next=http://example.com&access_token=xxx

The access_token is the returned by facebook, in the example the omniauth["credentials"]["token"].

Reference 1

@Ikhan
Copy link

Ikhan commented Sep 8, 2015

Hi,

Is there any way to redirect to the app than redirecting to the facebook page, once its logged out.

Thanks.

@Amit-Thawait
Copy link

@1mrankhan Yes, use the url param named next in your redirect_to url

See line 5 in this example : https://gist.github.com/rdetert/848721#file-application_controller-rb-L5

redirect_to "http://www.facebook.com/logout.php?api_key=#{fb_api_key}&session_key=#{fb_session_key}&confirm=1&next=#{destroy_user_session_url}"
                                                                                                              ^^^^

@D4v1dW3bb
Copy link

This problem took me one day. All information was scattered around the web and was focused only on single Facebook login. I needed also to login with local and other social media accounts. So this is my working solution, hope it helps someone:

in applicatoin.html.erb:
change:

<% else %> 
     <%= link_to "Sign out", facebook_logout_path %>
<% end %>

to

<% else %>
  <% if session[:fb_token].nil? %>
        <li><%= link_to "Sign out", facebook_logout_path %></li>
  <% else %>
        <li> <%= link_to "Sign out", facebook_logout_path, id: "sign_out_fb" %></li>
  <% end %>
<% end %>

make file app/javascript/facebook.js.coffee.erb

jQuery ->
  $('body').prepend('<div id="fb-root"></div>')

  $.ajax
    url: "#{window.location.protocol}//connect.facebook.net/en_US/all.js"
    dataType: 'script'
    cache: true

window.fbAsyncInit = ->
  FB.init(appId: '<%= ENV["FACEBOOK_APP_ID"] %>', cookie: true)

  $('#sign_in_fb').click (e) ->
    e.preventDefault()
    FB.login (response) ->
      window.location = '/auth/facebook/callback' if response.authResponse

  $('#sign_out_fb').click (e) ->
    FB.getLoginStatus (response) ->
       FB.logout() if response.authResponse
    true

generate the devise views:

$ rails g devise:views User

in user/shared/_links.html.erb

change:


<%- resource_class.omniauth_providers.each do |provider| %>
     <%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider) %><br />
<% end %>

to

<%- resource_class.omniauth_providers.each do |provider| %>
    <% if provider == 'facebook'%>
      <%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider), id: "sign_in_fb" %><br />
    <% else %>
      <%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider) %><br />
    <% end %>
  <% end -%>

notes

  • I had to add , :method => :delete at the end of both <%= link_to "Sign out" ...... %> lines

@devyani9999
Copy link

devyani9999 commented Jul 5, 2017

A simple way to logout from facebook from your app's server side is to redirect it to facebook.com/logout
In logout method, add
redirect_to "https://www.facebook.com/logout.php?next=#{redirection_url}&access_token=#{fb_token}"
where, redirection url, is the url where facebook would redirect/callback after logout. For example, "http://www.example.com:3000/thankyou"
fb_token, is the token you get from auth_hash['credentials']['token'] , where auth_hash = request.env['omniauth.auth']

PS: omniauth-facebook gem is being used here (https://github.com/mkdynamic/omniauth-facebook)

Thanks
Devyani@livvel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment