Created
March 1, 2011 06:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Users::SessionsController < Devise::SessionsController | |
def destroy | |
super | |
session[:fb_token] = nil | |
end | |
end |
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
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:
to
make file app/javascript/facebook.js.coffee.erb
generate the devise views:
$ rails g devise:views User
in user/shared/_links.html.erb
change:
to
notes
, :method => :delete
at the end of both<%= link_to "Sign out" ...... %>
lines