1- // Copyright Google Inc.
2- //
1+ // Copyright 2017 Google LLC
2+
33// Licensed under the Apache License, Version 2.0 (the "License");
44// you may not use this file except in compliance with the License.
55// You may obtain a copy of the License at
6- //
6+
77// http://www.apache.org/licenses/LICENSE-2.0
8- //
8+
99// Unless required by applicable law or agreed to in writing, software
1010// distributed under the License is distributed on an "AS IS" BASIS,
1111// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -22,9 +22,11 @@ function getStatus() {
2222 return response . text ( ) ;
2323 }
2424 // [START handle_error]
25+ // [START gae_handle_error]
2526 if ( response . status === 401 ) {
2627 statusElm . innerHTML = 'Login stale. <input type="button" value="Refresh" onclick="sessionRefreshClicked();"/>' ;
2728 }
29+ // [END gae_handle_error]
2830 // [END handle_error]
2931 else {
3032 statusElm . innerHTML = response . statusText ;
@@ -42,6 +44,7 @@ getStatus();
4244setInterval ( getStatus , 10000 ) ; // 10 seconds
4345
4446// [START refresh_session]
47+ // [START gae_refresh_session]
4548var iapSessionRefreshWindow = null ;
4649
4750function sessionRefreshClicked ( ) {
@@ -54,16 +57,29 @@ function sessionRefreshClicked() {
5457
5558function checkSessionRefresh ( ) {
5659 if ( iapSessionRefreshWindow != null && ! iapSessionRefreshWindow . closed ) {
57- fetch ( '/favicon.ico' ) . then ( function ( response ) {
60+ // Attempting to start a new session.
61+ // XMLHttpRequests is used by the server to identify AJAX requests
62+ fetch ( '/favicon.ico' , {
63+ method : "GET" ,
64+ credentials : 'include' ,
65+ headers : {
66+ 'X-Requested-With' : 'XMLHttpRequest'
67+ }
68+ . then ( ( response ) => {
69+ // Checking if browser has a session for the requested app
5870 if ( response . status === 401 ) {
71+ // No new session detected. Try to get a session again
5972 window . setTimeout ( checkSessionRefresh , 500 ) ;
6073 } else {
74+ // Session retrieved.
6175 iapSessionRefreshWindow . close ( ) ;
6276 iapSessionRefreshWindow = null ;
6377 }
78+ } )
6479 } ) ;
6580 } else {
6681 iapSessionRefreshWindow = null ;
6782 }
6883}
84+ // [END gae_refresh_session]
6985// [END refresh_session]
0 commit comments