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
|
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
|
|
|
|
2015-12-11 17:47:42 -05:00
|
|
|
if @waitingForLogin and @loggedIn
|
2015-12-11 18:39:07 -05:00
|
|
|
@fetchMeForLogin()
|
2016-01-14 13:23:16 -05:00
|
|
|
|
2015-12-11 17:47:42 -05:00
|
|
|
loginThroughFacebook: ->
|
|
|
|
if @loggedIn
|
|
|
|
@fetchMeForLogin()
|
|
|
|
else
|
2016-01-26 18:05:27 -05:00
|
|
|
FB.login ((response) ->
|
|
|
|
console.log 'Received FB login response:', response
|
|
|
|
), scope: 'email'
|
2015-12-11 17:47:42 -05:00
|
|
|
@waitingForLogin = true
|
2016-01-14 13:23:16 -05:00
|
|
|
|
2015-12-11 17:47:42 -05:00
|
|
|
fetchMeForLogin: ->
|
2016-01-14 13:23:16 -05:00
|
|
|
FB.api('/me', {fields: 'email,last_name,first_name,gender'}, @onReceiveMeInfo)
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
onReceiveMeInfo: (r) =>
|
2016-01-20 11:10:01 -05:00
|
|
|
console.log "Got Facebook user info:", r
|
2014-01-03 13:32:13 -05:00
|
|
|
unless r.email
|
|
|
|
console.error('could not get data, since no email provided')
|
|
|
|
return
|
|
|
|
|
|
|
|
oldEmail = me.get('email')
|
2014-06-11 14:34:52 -04:00
|
|
|
me.set('firstName', r.first_name) if r.first_name
|
|
|
|
me.set('lastName', r.last_name) if r.last_name
|
|
|
|
me.set('gender', r.gender) if r.gender
|
|
|
|
me.set('email', r.email) if r.email
|
|
|
|
me.set('facebookID', r.id) if r.id
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-08-27 15:24:03 -04:00
|
|
|
Backbone.Mediator.publish 'auth:logging-in-with-facebook', {}
|
2014-01-03 13:32:13 -05:00
|
|
|
window.tracker?.identify()
|
2014-12-08 18:00:49 -05:00
|
|
|
beforeID = me.id
|
2014-06-11 14:34:52 -04:00
|
|
|
me.patch({
|
2014-01-03 13:32:13 -05:00
|
|
|
error: backboneFailure,
|
2014-06-11 14:34:52 -04:00
|
|
|
url: "/db/user/#{me.id}?facebookID=#{r.id}&facebookAccessToken=#{@authResponse.accessToken}"
|
2014-01-03 13:32:13 -05:00
|
|
|
success: (model) ->
|
2015-02-27 19:07:41 -05:00
|
|
|
window.tracker?.trackEvent 'Facebook Login', category: "Signup", label: 'Facebook'
|
2014-12-08 18:00:49 -05:00
|
|
|
if model.id is beforeID
|
|
|
|
window.tracker?.trackEvent 'Finished Signup', category: "Signup", label: 'Facebook'
|
2014-01-03 13:32:13 -05:00
|
|
|
window.location.reload() if model.get('email') isnt oldEmail
|
|
|
|
})
|
|
|
|
|
|
|
|
destroy: ->
|
|
|
|
super()
|