Skip to content

Instantly share code, notes, and snippets.

@philcockfield
Created December 3, 2012 07:42

Revisions

  1. philcockfield revised this gist Dec 3, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion auth.coffee
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,6 @@ do ->
    css

    tmpl.label = ->
    # return 'Loading...' if Session.get('fb-loading') is true
    switch ns.signedIn()
    when true then 'Sign Out'
    when false then 'Sign In'
  2. philcockfield created this gist Dec 3, 2012.
    35 changes: 35 additions & 0 deletions auth.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    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 = ->
    # return 'Loading...' if Session.get('fb-loading') is true
    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()
    4 changes: 4 additions & 0 deletions auth.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    <template name='core-facebook-btn-login'>
    <button class='core btn fb login {{class}}'>{{ label }}</button>
    </template>

    12 changes: 12 additions & 0 deletions auth_server.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    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
    49 changes: 49 additions & 0 deletions facebook_auth_status.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    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





    34 changes: 34 additions & 0 deletions meteor_auth_status.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    ###
    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