codecombat/app/core/social-handlers/FacebookHandler.coffee

57 lines
1.4 KiB
CoffeeScript
Raw Normal View History

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:
'auth:logged-in-with-facebook': 'onFacebookLoggedIn'
loggedIn: false
token: -> @authResponse?.accessToken
fakeFacebookLogin: ->
@onFacebookLoggedIn({
response:
authResponse: { accessToken: '1234' }
})
2014-01-03 13:32:13 -05: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
@loggedIn = false
2014-01-03 13:32:13 -05:00
@authResponse = e.response.authResponse
for fbProp, userProp of userPropsToSave
unless me.get(userProp)
@loggedIn = true
2014-01-03 13:32:13 -05:00
break
@trigger 'logged-into-facebook'
loginThroughFacebook: ->
if @loggedIn
return true
else
FB.login ((response) ->
console.log 'Received FB login response:', response
), scope: 'email'
2014-01-03 13:32:13 -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