public
Created — forked from rdetert/application.html.erb

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

  • Download Gist
application.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<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>
application_controller.rb
Ruby
1 2 3 4 5 6
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
authentications_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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
routes.rb
Ruby
1 2 3 4 5 6
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'}
sessions_controller.rb
Ruby
1 2 3 4 5 6 7 8
class Users::SessionsController < Devise::SessionsController
def destroy
super
session[:fb_token] = nil
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.