Search code examples
ruby-on-railsfacebookkoala

How to check if you are logged into Facebook in the GEM koala


The application is written in Ruby on Rails, using Koala GEM to support Facebook auth.

How to check if a given user. log on to our app now.

I have a code to login

def index
  @oauth = Koala::Facebook::OAuth.new (APP_ID, SECRET, URL_US)
  @a = @oauth.url_for_oauth_code (:permissions => "email")
  redirect_to @a
end

I would if I worked Koala already logged as such it calls redirect_to client_app_path and how not to redirect_to @a


Solution

  • You could use this:

    def index
      @oauth = Koala::Facebook::OAuth.new (APP_ID, SECRET, URL_US)
      facebook_user_id = @oauth.get_user_from_cookies(cookies)
      @a = @oauth.url_for_oauth_code (:permissions => "email")
      if facebook_user_id.present?
        redirect_to client_app_path
      else
        redirect_to @a
      end
    end
    

    I am not sure of the output of @oauth.get_user_from_cookies(cookies) if you are not connected to Facebook, maybe it's an empty string instead of nil. If so, change the if statement to redirect properly if connected or not.

    Seen here: https://github.com/arsduo/koala/wiki/OAuth in Cookie Parsing