3
votes

I am trying to get my logout button to log out of both the app and facebook based off of the instructions shown in railscast #360. When I click on it, the app appears to logout but my facebook session persists. Once every maybe five times, the user IS logged out of facebook. I suspect that this has something to do with the facebook cookie not being destroyed when the FB.logout() function is called, because when I delete that cookie manually, the user is logged out of facebook. Perhaps it is a timing issue since this is asynchronous? Any suggestions? Here is the relevant code:

application.html.erb

    <div id="fb-root"></div>
    <script>

    window.fbAsyncInit = function() {
        FB.init({
            appId      : '(**myappid**)', // App ID
            status     : true, // check login status
            cookie     : true // enable cookies to allow the server to access the session
        });

        $('#sign_in').click(function(e) {
            e.preventDefault();
            return FB.login(function(response) {
                if (response.authResponse) {
                    return window.location = '/auth/facebook/callback';
                }
            });
        });

        return $('#sign_out').click(function(e) {
            FB.getLoginStatus(function(response) {
                if (response.authResponse) {
                    return FB.logout();
                }
            });
            return true;
        });
    };
     </script>

sessions_controller.rb

class SessionsController < ApplicationController

      def create
        user = User.from_omniauth(env["omniauth.auth"])
        session[:user_id] = user.id
        redirect_to root_url
      end

      def destroy
        session[:user_id] = nil
        redirect_to root_url
      end
    end
1

1 Answers

2
votes

Fixed this by adding:

  def destroy
    session[:user_id] = nil
    sleep 3
    redirect_to root_url
  end

to the sessions controller