@@ -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+ }
0 commit comments