2014-11-28 20:49:41 -05:00
|
|
|
CocoClass = require 'core/CocoClass'
|
|
|
|
{me} = require 'core/auth'
|
|
|
|
{backboneFailure} = require 'core/errors'
|
|
|
|
storage = require 'core/storage'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
# facebook user object props to
|
|
|
|
userPropsToSave =
|
|
|
|
'first_name': 'firstName'
|
|
|
|
'last_name': 'lastName'
|
|
|
|
'gender': 'gender'
|
|
|
|
'email': 'email'
|
|
|
|
'id': 'facebookID'
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = FacebookHandler = class FacebookHandler extends CocoClass
|
|
|
|
subscriptions:
|
2014-08-27 15:24:03 -04:00
|
|
|
'auth:logged-in-with-facebook': 'onFacebookLoggedIn'
|
2016-01-14 13:23:16 -05:00
|
|
|
|
2015-12-11 17:47:42 -05:00
|
|
|
loggedIn: false
|
2016-02-25 18:24:16 -05:00
|
|
|
|
|
|
|
token: -> @authResponse?.accessToken
|
|
|
|
|
|
|
|
fakeFacebookLogin: ->
|
|
|
|
@onFacebookLoggedIn({
|
|
|
|
response:
|
|
|
|
authResponse: { accessToken: '1234' }
|
|
|
|
})
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-08-27 15:24:03 -04:00
|
|
|
onFacebookLoggedIn: (e) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
# user is logged in also when the page first loads, so check to see
|
|
|
|
# if we really need to do the lookup
|
2015-12-11 17:47:42 -05:00
|
|
|
@loggedIn = false
|
2014-01-03 13:32:13 -05:00
|
|
|
@authResponse = e.response.authResponse
|
|
|
|
for fbProp, userProp of userPropsToSave
|
|
|
|
unless me.get(userProp)
|
2015-12-11 17:47:42 -05:00
|
|
|
@loggedIn = true
|
2014-01-03 13:32:13 -05:00
|
|
|
break
|
2016-01-14 13:23:16 -05:00
|
|
|
|
2016-02-25 18:24:16 -05:00
|
|
|
@trigger 'logged-into-facebook'
|
2016-01-14 13:23:16 -05:00
|
|
|
|
2015-12-11 17:47:42 -05:00
|
|
|
loginThroughFacebook: ->
|
|
|
|
if @loggedIn
|
2016-02-25 18:24:16 -05:00
|
|
|
return true
|
2015-12-11 17:47:42 -05:00
|
|
|
else
|
2016-01-26 18:05:27 -05:00
|
|
|
FB.login ((response) ->
|
|
|
|
console.log 'Received FB login response:', response
|
|
|
|
), scope: 'email'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2016-02-25 18:24:16 -05:00
|
|
|
loadPerson: ->
|
|
|
|
FB.api '/me', {fields: 'email,last_name,first_name,gender'}, (person) =>
|
|
|
|
attrs = {}
|
|
|
|
for fbProp, userProp of userPropsToSave
|
|
|
|
value = person[fbProp]
|
|
|
|
if value
|
|
|
|
attrs[userProp] = value
|
|
|
|
@trigger 'person-loaded', attrs
|