Created
December 3, 2012 07:42
-
-
Save philcockfield/4193430 to your computer and use it in GitHub Desktop.
Meteor - Auth with FB JavaScript SDK
This file contains hidden or 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
do -> | |
# See: http://developers.facebook.com/docs/reference/javascript/ | |
ns = APP.ns 'core.facebook' | |
# ---------------------- Methods ---------------------- | |
ns.login = (callback) -> FB.login -> callback?() | |
ns.logout = (callback) -> FB.logout -> callback?() | |
ns.signedIn = -> authStatus() is 'signed-in' | |
authStatus = -> Session.get('auth-status') | |
# ---------------------- Templates ---------------------- | |
tmpl = Template['core-facebook-btn-login'] | |
tmpl.class = -> | |
css = '' | |
css += 'hidden' if authStatus() is 'unknown' | |
css | |
tmpl.label = -> | |
switch ns.signedIn() | |
when true then 'Sign Out' | |
when false then 'Sign In' | |
tmpl.events = | |
'click button.fb.login': -> | |
switch ns.signedIn() | |
when true then ns.logout() | |
when false then ns.login() |
This file contains hidden or 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
<template name='core-facebook-btn-login'> | |
<button class='core btn fb login {{class}}'>{{ label }}</button> | |
</template> |
This file contains hidden or 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
Meteor.methods | |
facebookLogin: (fbUser, accessToken) -> | |
serviceData = | |
id: fbUser.id | |
accessToken: accessToken | |
email: fbUser.email | |
options = | |
profile: | |
name: fbUser.name | |
userId = Accounts.updateOrCreateUserFromExternalService('facebook', serviceData, options).id |
This file contains hidden or 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
do -> | |
ns = APP.ns 'core.facebook' | |
### | |
Monitors the Facebook SDK and provides a consistent set of | |
status updates. | |
### | |
ns._facebookAuthStatus = -> | |
# Wire up events. | |
FB.Event.subscribe 'auth.authResponseChange', (res) -> updateStatus() | |
FB.getLoginStatus (res) -> updateStatus() | |
getFacebookUser = (callback) -> | |
if ns.user? | |
callback?(ns.user) | |
else | |
FB.api '/me', (fbUser) -> | |
ns.user = fbUser | |
Session.set 'fb-user', fbUser | |
callback?(fbUser) | |
setStatus = (value) -> Session.set 'fb-auth-status', value | |
updateStatus = -> | |
authResponse = FB.getAuthResponse() | |
if authResponse is undefined | |
setStatus 'unknown' | |
else if authResponse is null | |
reset() | |
setStatus 'signed-out' | |
else | |
Session.set 'fb-token', authResponse.accessToken | |
if ns.user? | |
setStatus 'signed-in' | |
else | |
setStatus 'pending-user' | |
getFacebookUser -> updateStatus() | |
reset = -> | |
ns.user = null | |
Session.set 'fb-user', null | |
Session.set 'fb-auth-token', null | |
This file contains hidden or 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
### | |
Manages the Meteor authentication status, which involves authenticating | |
against the Meteor backend after the Facebook SDK has completed it's | |
authentication steps. | |
### | |
do -> | |
ns = APP.ns 'core.facebook' | |
setStatus = (value) -> Session.set 'auth-status', value | |
setStatus 'unknown' | |
SIGNED_IN = 'signed-in' | |
SIGNED_OUT = 'signed-out' | |
serverLogin = (callback) -> | |
fbUser = Session.get('fb-user') | |
token = Session.get('fb-token') | |
Meteor.call 'facebookLogin', fbUser, token, (err, userId) -> | |
# Hook into the core Meteor account system, and pop | |
# the client into a state of being logged in. | |
Accounts._makeClientLoggedIn userId, token | |
callback?() | |
Meteor.autorun -> | |
switch Session.get( 'fb-auth-status' ) | |
when SIGNED_IN | |
serverLogin -> setStatus SIGNED_IN | |
when SIGNED_OUT | |
Meteor.logout -> setStatus SIGNED_OUT | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment