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-03-23 12:30:01 -04:00
|
|
|
GPLUS_TOKEN_KEY = 'gplusToken'
|
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='
|
2014-06-30 22:16:26 -04:00
|
|
|
clientID = '800329290710-j9sivplv2gpcdgkrsis9rff3o417mlfa.apps.googleusercontent.com'
|
2015-02-13 14:24:06 -05:00
|
|
|
scope = 'https://www.googleapis.com/auth/plus.login email'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
module.exports = GPlusHandler = class GPlusHandler extends CocoClass
|
|
|
|
constructor: ->
|
2015-04-18 22:41:34 -04:00
|
|
|
@accessToken = storage.load GPLUS_TOKEN_KEY, false
|
2016-02-25 18:24:16 -05:00
|
|
|
window.onGPlusLogin = _.bind(@onGPlusLogin, @)
|
2014-01-03 13:32:13 -05:00
|
|
|
super()
|
|
|
|
|
2016-02-25 18:24:16 -05:00
|
|
|
token: -> @accessToken?.access_token
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2016-02-25 18:24:16 -05:00
|
|
|
loadAPI: ->
|
|
|
|
return if @loadedAPI
|
|
|
|
@loadedAPI = true
|
|
|
|
(=>
|
|
|
|
po = document.createElement('script')
|
|
|
|
po.type = 'text/javascript'
|
|
|
|
po.async = true
|
|
|
|
po.src = 'https://apis.google.com/js/client:platform.js?onload=onGPlusLoaded'
|
|
|
|
s = document.getElementsByTagName('script')[0]
|
|
|
|
s.parentNode.insertBefore po, s
|
|
|
|
window.onGPlusLoaded = _.bind(@onLoadAPI, @)
|
|
|
|
return
|
|
|
|
)()
|
|
|
|
|
|
|
|
onLoadAPI: ->
|
|
|
|
Backbone.Mediator.publish 'auth:gplus-api-loaded', {}
|
2014-03-23 12:30:01 -04:00
|
|
|
session_state = null
|
2014-11-14 14:04:17 -05:00
|
|
|
if @accessToken and me.get('gplusID')
|
2014-03-23 12:30:01 -04:00
|
|
|
# We need to check the current state, given our access token
|
|
|
|
gapi.auth.setToken 'token', @accessToken
|
|
|
|
session_state = @accessToken.session_state
|
2014-06-30 22:16:26 -04:00
|
|
|
gapi.auth.checkSessionState({client_id: clientID, session_state: session_state}, @onCheckedSessionState)
|
2014-03-23 12:30:01 -04:00
|
|
|
else
|
|
|
|
# If we ran checkSessionState, it might return true, that the user is logged into Google, but has not authorized us
|
|
|
|
@loggedIn = false
|
|
|
|
func = => @trigger 'checked-state'
|
|
|
|
setTimeout func, 1
|
|
|
|
|
2016-02-25 18:24:16 -05:00
|
|
|
renderLoginButtons: ->
|
|
|
|
return false unless gapi?.plusone?
|
|
|
|
gapi.plusone.go?() # Handles +1 button
|
|
|
|
if not gapi.signin?.render
|
|
|
|
console.warn 'Didn\'t have gapi.signin to render G+ login button. (DoNotTrackMe extension?)'
|
|
|
|
return
|
|
|
|
|
|
|
|
for gplusButton in $('.gplus-login-button')
|
|
|
|
params = {
|
|
|
|
callback: 'onGPlusLogin',
|
|
|
|
clientid: clientID,
|
|
|
|
cookiepolicy: 'single_host_origin',
|
|
|
|
scope: 'https://www.googleapis.com/auth/plus.login email',
|
|
|
|
height: 'short',
|
|
|
|
}
|
|
|
|
if gapi.signin?.render
|
|
|
|
gapi.signin.render(gplusButton, params)
|
|
|
|
|
|
|
|
@trigger 'render-login-buttons'
|
|
|
|
|
2014-03-23 12:30:01 -04:00
|
|
|
onCheckedSessionState: (@loggedIn) =>
|
|
|
|
@trigger 'checked-state'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-03-23 12:30:01 -04:00
|
|
|
reauthorize: ->
|
|
|
|
params =
|
|
|
|
'client_id' : clientID
|
|
|
|
'scope' : scope
|
|
|
|
gapi.auth.authorize params, @onGPlusLogin
|
2016-02-25 18:24:16 -05:00
|
|
|
|
|
|
|
fakeGPlusLogin: ->
|
|
|
|
@onGPlusLogin({
|
|
|
|
access_token: '1234'
|
|
|
|
})
|
2014-04-13 17:48:36 -04:00
|
|
|
|
2016-02-25 18:24:16 -05:00
|
|
|
onGPlusLogin: (e) ->
|
|
|
|
return unless e.access_token
|
2014-03-23 12:30:01 -04:00
|
|
|
@loggedIn = true
|
2016-02-25 18:24:16 -05:00
|
|
|
Backbone.Mediator.publish 'auth:logged-in-with-gplus', e
|
2014-11-14 14:04:17 -05:00
|
|
|
try
|
|
|
|
# Without removing this, we sometimes get a cross-domain error
|
2015-01-09 15:56:48 -05:00
|
|
|
d = _.omit(e, 'g-oauth-window')
|
2015-04-18 22:41:34 -04:00
|
|
|
storage.save(GPLUS_TOKEN_KEY, d, 0)
|
2014-11-14 14:04:17 -05:00
|
|
|
catch e
|
|
|
|
console.error 'Unable to save G+ token key', e
|
2014-03-23 12:30:01 -04:00
|
|
|
@accessToken = e
|
|
|
|
@trigger 'logged-in'
|
2016-02-25 18:24:16 -05:00
|
|
|
@trigger 'logged-into-google'
|
2014-12-20 19:02:41 -05:00
|
|
|
|
2016-02-25 18:24:16 -05:00
|
|
|
loadPerson: (options={}) ->
|
|
|
|
@reloadOnLogin = options.reloadOnLogin
|
2014-01-03 13:32:13 -05:00
|
|
|
# email and profile data loaded separately
|
2015-02-13 14:18:00 -05:00
|
|
|
gapi.client.load('plus', 'v1', =>
|
|
|
|
gapi.client.plus.people.get({userId: 'me'}).execute(@onPersonReceived))
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2015-02-13 14:18:00 -05:00
|
|
|
onPersonReceived: (r) =>
|
2016-02-25 18:24:16 -05:00
|
|
|
attrs = {}
|
2014-01-03 13:32:13 -05:00
|
|
|
for gpProp, userProp of userPropsToSave
|
|
|
|
keys = gpProp.split('.')
|
|
|
|
value = r
|
2014-12-20 19:02:41 -05:00
|
|
|
for key in keys
|
|
|
|
value = value[key]
|
2016-02-25 18:24:16 -05:00
|
|
|
if value
|
|
|
|
attrs[userProp] = value
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2015-02-13 14:18:00 -05:00
|
|
|
newEmail = r.emails?.length and r.emails[0] isnt me.get('email')
|
2014-11-14 14:04:17 -05:00
|
|
|
return unless newEmail or me.get('anonymous', true)
|
2016-02-25 18:24:16 -05:00
|
|
|
if r.emails?.length
|
|
|
|
attrs.email = r.emails[0].value
|
|
|
|
@trigger 'person-loaded', attrs
|
2014-04-13 17:48:36 -04:00
|
|
|
|
2014-03-23 12:30:01 -04:00
|
|
|
loadFriends: (friendsCallback) ->
|
|
|
|
return friendsCallback() unless @loggedIn
|
2014-04-13 17:48:36 -04:00
|
|
|
expiresIn = if @accessToken then parseInt(@accessToken.expires_at) - new Date().getTime()/1000 else -1
|
2014-06-30 22:16:26 -04:00
|
|
|
onReauthorized = => gapi.client.request({path: '/plus/v1/people/me/people/visible', callback: friendsCallback})
|
2014-04-13 17:48:36 -04:00
|
|
|
if expiresIn < 0
|
|
|
|
# TODO: this tries to open a popup window, which might not ever finish or work, so the callback may never be called.
|
2014-03-23 12:30:01 -04:00
|
|
|
@reauthorize()
|
|
|
|
@listenToOnce(@, 'logged-in', onReauthorized)
|
|
|
|
else
|
|
|
|
onReauthorized()
|