Skip to content

Commit d466c77

Browse files
committed
improved G+ action
1 parent 9d1dec8 commit d466c77

3 files changed

Lines changed: 111 additions & 14 deletions

File tree

‎v3/composingprograms.html‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,44 @@
7676
<table id="experimentalHeader" style="margin-bottom: 15pt;">
7777

7878

79-
<span id="signinButton">
80-
<span
79+
<div id="gplusDiv" style="margin-bottom: 10pt;">
80+
81+
<div id="signinButton" style="font-size: 8pt;">
82+
<button
8183
class="g-signin"
8284
data-callback="signinCallback"
8385
data-clientid="639826413051-n4c566bfu9jnee6893qolvbt8od9kiaf.apps.googleusercontent.com"
8486
data-cookiepolicy="single_host_origin"
8587
data-requestvisibleactions="http://schemas.google.com/AddActivity"
8688
data-scope="https://www.googleapis.com/auth/plus.profile.emails.read">
87-
</span>
88-
</span>
89+
</button>
90+
91+
Sign in with either your UC Berkeley email or personal Google+
92+
account.
93+
94+
</div>
95+
96+
<div id="loggedInDiv" style="display: none;">
97+
<span id="loggedInNameDisplay"></span>
98+
<button id="signoutButton" style="font-size: 10pt;">Sign out</button>
99+
</div>
100+
101+
</div>
102+
103+
104+
<div id="authOps" style="display:none">
105+
<h2>User is now signed in to the app using Google+</h2>
106+
<p>If the user chooses to disconnect, the app must delete all stored
107+
information retrieved from Google for the given user.</p>
108+
<button id="disconnect" >Disconnect your Google account from this app</button>
109+
110+
<h2>User's profile information</h2>
111+
<div id="profile"></div>
112+
113+
<h2>Authentication Logs</h2>
114+
<pre id="authResult"></pre>
115+
</div>
116+
89117

90118

91119
<tr>

‎v3/js/composingprograms.js‎

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ function executeCode(forceStartingInstr, forceRawInputLst) {
8080
}
8181

8282
$(document).ready(function() {
83+
$('#signoutButton').click(signout);
8384

8485
genericOptFrontendReady(); // initialize at the very end
8586
});
@@ -92,30 +93,73 @@ function signinCallback(authResult) {
9293
if (authResult) {
9394
if (authResult['error'] == undefined){
9495

96+
$("#signinButton").hide();
97+
$("#loggedInDiv").show();
98+
9599
// This sample assumes a client object has been created.
96100
// To learn more about creating a client, check out the starter:
97101
// https://developers.google.com/+/quickstart/javascript
98-
gapi.client.load('plus','v1', function(){
99-
var request = gapi.client.plus.people.get({
100-
'userId': 'me'
101-
});
102-
request.execute(function(resp) {
102+
gapi.client.load('plus','v1', function() {
103+
var request = gapi.client.plus.people.get({
104+
'userId': 'me'
105+
});
106+
request.execute(function(resp) {
103107
// From https://developers.google.com/+/web/people/#retrieve_an_authenticated_users_email_address
104108

105109
// Filter the emails object to find the user's primary account, which might
106110
// not always be the first in the array. The filter() method supports IE9+.
107111
var email = resp['emails'].filter(function(v) {
108-
return v.type === 'account'; // Filter out the primary email
112+
return v.type === 'account'; // Filter out the primary email
109113
})[0].value; // get the email from the filtered results, should always be defined.
110114

111-
alert('Real name: "' + resp.displayName + '", Email addr: "' + email + '"');
112-
});
115+
//alert('Real name: "' + resp.displayName + '", Email addr: "' + email + '"');
116+
117+
// if we can actually grab the display name (e.g., from a
118+
// Google+ account), then use it
119+
if (resp.displayName) {
120+
$("#loggedInNameDisplay").html("Hello, " + resp.displayName);
121+
}
122+
123+
// otherwise try to look up the email address on the server to
124+
// find a real name mapping on the server
125+
else if (email){
126+
$.get('name_lookup.py',
127+
{email : email},
128+
function(dataFromBackend) {
129+
$("#loggedInNameDisplay").html("Hello, " + email);
130+
},
131+
"json");
132+
}
133+
});
113134
});
114135

115136
} else {
116-
console.log('An error occurred');
137+
console.log('signinCallback: error occurred');
117138
}
118139
} else {
119-
console.log('Empty authResult'); // Something went wrong
140+
console.log('signinCallback: empty authResult'); // Something went wrong
120141
}
121142
}
143+
144+
// adapted from https://developers.google.com/+/quickstart/javascript
145+
/**
146+
* Calls the OAuth2 endpoint to disconnect the app for the user.
147+
*/
148+
function signout() {
149+
// Revoke the access token.
150+
$.ajax({
151+
type: 'GET',
152+
url: 'https://accounts.google.com/o/oauth2/revoke?token=' +
153+
gapi.auth.getToken().access_token,
154+
async: false,
155+
contentType: 'application/json',
156+
dataType: 'jsonp',
157+
success: function(result) {
158+
$('#signinButton').show();
159+
$('#loggedInDiv').hide();
160+
},
161+
error: function(e) {
162+
console.log(e);
163+
}
164+
});
165+
}

‎v3/name_lookup.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/local/bin/python2.7
2+
3+
# Minimal CGI script for name lookups
4+
5+
# Also, check CGI execute permission in your script directory.
6+
# You might need to create an .htaccess file like the following:
7+
#
8+
# Options +ExecCGI
9+
# AddHandler cgi-script .py
10+
11+
import cgi
12+
import json
13+
import sys
14+
15+
form = cgi.FieldStorage()
16+
requested_email = form['email'].value
17+
18+
print("Content-type: text/plain; charset=iso-8859-1\n")
19+
20+
for name, email in csv.reader(open('names.csv')):
21+
if email == requested_email:
22+
print(json.dumps({'name': name, 'email': email}))
23+
sys.exit(0) # get out early
24+
25+
print(json.dumps({'error': 1}))

0 commit comments

Comments
 (0)