Skip to content

Instantly share code, notes, and snippets.

@shavit
Created February 28, 2013 11:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shavit/5055943 to your computer and use it in GitHub Desktop.
Save shavit/5055943 to your computer and use it in GitHub Desktop.
Facebook JavaScript SDK in CoffeeScript
#
# You should add the Facebook App ID and the channel url (optional), in the #fb-root element, as a data- attribute:
# <div id="fb-root" data-app-id="<%= ENV['FACEBOOK_APP_ID'] %>" data-channel-url="<%= url_no_scheme('/channel.html') %>"></div>
#
window.fbAsyncInit = ->
FB.init
appId: document.getElementById("fb-root").getAttribute("data-app-id")
channelUrl: document.getElementById("fb-root").getAttribute("data-channel-url")
status: true,
cookie: true,
xfbml: true
FB.Event.subscribe('auth.login', (response) ->
window.location = window.location
)
FB.Canvas.setAutoGrow()
FB.getLoginStatus((data) ->
if (data.status == "connected")
uid = data.authResponse.userID
accessToken = data.authResponse.accessToken;
FB.api("/me", (data) ->
console.log("Hello #{data.name}")
)
else
if (response.status == "not_authorized")
# the user is logged in to Facebook,
# but has not authenticated your app
else
# the user isn't logged in to Facebook.
)
PageScript = document.getElementsByTagName("script")[0]
return if document.getElementById("FBScript")
FBScript = document.createElement("script")
FBScript.id = "FBScript"
FBScript.async = true
FBScript.src = "//connect.facebook.net/en_US/all.js"
PageScript.parentNode.insertBefore(FBScript, PageScript)
@itsluke
Copy link

itsluke commented Jun 26, 2014

Just a quick note,

if (response.status == "not_authorized")

should read

if (data.status == "not_authorized")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment