Skip to content

Instantly share code, notes, and snippets.

@rdetert
Created March 1, 2011 06:28
Show Gist options
  • Select an option

  • Save rdetert/848721 to your computer and use it in GitHub Desktop.

Select an option

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
@Ikhan
Copy link
Copy Markdown

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
Copy Markdown

@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
Copy Markdown

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
Copy Markdown

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