codecombat/app/lib/GPlusHandler.coffee

79 lines
2.4 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
CocoClass = require 'lib/CocoClass'
{me, CURRENT_USER_KEY} = require 'lib/auth'
{backboneFailure} = require 'lib/errors'
2014-01-26 17:44:08 -05:00
storage = require 'lib/storage'
2014-01-03 13:32:13 -05:00
# gplus user object props to
userPropsToSave =
'name.givenName': 'firstName'
'name.familyName': 'lastName'
'gender': 'gender'
'id': 'gplusID'
fieldsToFetch = 'displayName,gender,image,name(familyName,givenName),id'
plusURL = '/plus/v1/people/me?fields='+fieldsToFetch
revokeUrl = 'https://accounts.google.com/o/oauth2/revoke?token='
clientID = "800329290710-j9sivplv2gpcdgkrsis9rff3o417mlfa.apps.googleusercontent.com"
module.exports = GPlusHandler = class GPlusHandler extends CocoClass
constructor: ->
super()
subscriptions:
'gplus-logged-in':'onGPlusLogin'
onGPlusLogin: (e) =>
return if e._aa # this seems to show that it was auto generated on page load
return if not me
@accessToken = e.access_token
# email and profile data loaded separately
@responsesComplete = 0
gapi.client.request(path:plusURL, callback:@onPersonEntityReceived)
gapi.client.load('oauth2', 'v2', =>
gapi.client.oauth2.userinfo.get().execute(@onEmailReceived))
shouldSave: false
responsesComplete: 0
onPersonEntityReceived: (r) =>
for gpProp, userProp of userPropsToSave
keys = gpProp.split('.')
value = r
value = value[key] for key in keys
if value and not me.get(userProp)
@shouldSave = true
me.set(userProp, value)
@responsesComplete += 1
@saveIfAllDone()
onEmailReceived: (r) =>
newEmail = r.email and r.email isnt me.get('email')
return unless newEmail or me.get('anonymous')
me.set('email', r.email)
@shouldSave = true
@responsesComplete += 1
@saveIfAllDone()
saveIfAllDone: =>
return unless @responsesComplete is 2
return unless me.get('email') and me.get('gplusID')
Backbone.Mediator.publish('logging-in-with-gplus')
gplusID = me.get('gplusID')
window.tracker?.trackEvent 'Google Login'
window.tracker?.identify()
patch = {}
patch[key] = me.get(key) for gplusKey, key of userPropsToSave
patch._id = me.id
patch.email = me.get('email')
me.save(patch, {
patch: true
2014-01-03 13:32:13 -05:00
error: backboneFailure,
url: "/db/user?gplusID=#{gplusID}&gplusAccessToken=#{@accessToken}"
success: (model) ->
2014-01-26 17:44:08 -05:00
storage.save(CURRENT_USER_KEY, model.attributes)
2014-01-03 13:32:13 -05:00
window.location.reload()
})