This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathrender.js
134 lines (118 loc) · 4.23 KB
/
render.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2013 Google Inc. All Rights Reserved.
/**
* @fileoverview
* Provides methods for the utility methods needed to render the Google+
* Sign-In button.
*
* @author [email protected] (Danny Hermes)
*/
/** google global namespace for Google projects. */
var google = google || {};
/** devrel namespace for Google Developer Relations projects. */
google.devrel = google.devrel || {};
/** samples namespace for DevRel sample code. */
google.devrel.samples = google.devrel.samples || {};
/** TicTacToe namespace for this sample. */
google.devrel.samples.ttt = google.devrel.samples.ttt || {};
/**
* Client ID of the application (from the APIs Console).
* @type {string}
*/
google.devrel.samples.ttt.CLIENT_ID =
'YOUR-CLIENT-ID';
/**
* Scopes used by the application.
* @type {string}
*/
google.devrel.samples.ttt.SCOPES =
'https://www.googleapis.com/auth/userinfo.email ' +
'https://www.googleapis.com/auth/plus.login';
/**
* Parses email from the claim set of a JWT ID token.
*
* NOTE: We are not validating the ID token since from a trusted source.
* We are simply parsed the value from the JWT.
*
* See http://www.tbray.org/ongoing/When/201x/2013/04/04/ID-Tokens
* or
* http://openid.net/specs/openid-connect-messages-1_0.html#StandardClaims
* for more info.
*
* @param {string} idToken A base64 JWT containing a user ID token.
* @return {string} The email parsed from the claim set, else undefined
* if one can't be parsed.
*/
google.devrel.samples.ttt.getEmailFromIDToken = function(idToken) {
if (typeof idToken !== 'string') {
return;
}
var segments = idToken.split('.');
if (segments.length !== 3) {
return;
}
try {
var claimSet = JSON.parse(atob(segments[1]));
} catch (e) {
return;
}
if (claimSet.email && typeof claimSet.email === 'string') {
return claimSet.email;
}
}
/**
* Handles the Google+ Sign In response.
*
* Success calls google.devrel.samples.ttt.init. Failure makes the Sign-In
* button visible.
*
* @param {Object} authResult The contents returned from the Google+
* Sign In attempt.
*/
google.devrel.samples.ttt.signinCallback = function(authResult) {
var tokenEmail = google.devrel.samples.ttt.getEmailFromIDToken(
authResult.id_token);
if (authResult.access_token && tokenEmail) {
google.devrel.samples.ttt.init('//' + window.location.host + '/_ah/api',
tokenEmail);
document.getElementById('signinButtonContainer').classList.remove(
'visible');
document.getElementById('signedInStatus').classList.add('visible');
} else {
document.getElementById('signinButtonContainer').classList.add('visible');
document.getElementById('signedInStatus').classList.remove('visible');
if (!authResult.error) {
console.log('Unexpected result');
console.log(authResult);
} else if (authResult.error !== 'immediate_failed') {
console.log('Unexpected error occured: ' + authResult.error);
} else {
console.log('Immediate mode failed, user needs to click Sign In.');
}
}
};
/**
* Renders the Google+ Sign-in button using auth parameters.
*/
google.devrel.samples.ttt.render = function() {
gapi.signin.render('signinButton', {
'callback': google.devrel.samples.ttt.signinCallback,
'clientid': google.devrel.samples.ttt.CLIENT_ID,
'cookiepolicy': 'single_host_origin',
'requestvisibleactions': 'http://schemas.google.com/AddActivity',
'scope': google.devrel.samples.ttt.SCOPES
});
};
// A quirk of the JSONP callback of the plusone client makes it so
// our callback must exist as an element in window.
window['google.devrel.samples.ttt.render'] = google.devrel.samples.ttt.render;
// Recommended code to load Google+ JS library.
(function() {
var newScriptElement = document.createElement('script');
newScriptElement.type = 'text/javascript';
newScriptElement.async = true;
newScriptElement.src = 'https://apis.google.com/js/client:plusone.js' +
'?onload=google.devrel.samples.ttt.render';
var firstScriptElement = document.getElementsByTagName('script')[0];
firstScriptElement.parentNode.insertBefore(newScriptElement,
firstScriptElement);
})();