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