FacebookSession is a simple Rails plugin that complements the Facebook Javascript SDK. It is capable of loading the API, handling Facebook logins and parsing signed requests.
Installation
Add it to your Gemfile and run bundle install
:
gem 'facebook_session'
Then run the generator:
rails g facebook_session:install
Configuration
The default configuration relies on two environment variables being
set, FACEBOOK_APPLICATION_ID
and FACEBOOK_APPLICATION_SECRET
.
If you'd like, you can configure these directly in config/initializers/facebook_session.rb:
FacebookSession.configure(
application_id: '12345678',
application_secret: 'myapplication_secret'
)
Loading the Facebook SDK
Requiring facebook_session
in your assets pipeline will
automatically load the Facebook JS SDK asynchronously. It will also
make sure it works between page loads if you have Turbolinks enabled.
//= require facebook_session
It also provides a method named window.withFacebookAPI
that takes a
function and runs it as soon as the Facebook SDK is available (or
immediately if it's already loaded).
Example (in CoffeeScript):
$('.my-button').click -> withFacebookAPI -> FB.login(...)
You can skip loading the facebook_session
script if you already have
the SDK set up.
Facebook login
In your controllers, helpers and views, you can now do:
if facebook_session?
facebook_session.user_id # => Facebook user ID
end
Parsing signed requests
if facebook_signed_request?
logger.info facebook_signed_request.app_data
end
Copyright
Copyright (c) 2012 Manual design. See LICENSE for details.