1
- // Copyright Google Inc.
2
- //
1
+ // Copyright 2017 Google LLC
2
+
3
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
4
// you may not use this file except in compliance with the License.
5
5
// You may obtain a copy of the License at
6
- //
6
+
7
7
// http://www.apache.org/licenses/LICENSE-2.0
8
- //
8
+
9
9
// Unless required by applicable law or agreed to in writing, software
10
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -22,9 +22,11 @@ function getStatus() {
22
22
return response . text ( ) ;
23
23
}
24
24
// [START handle_error]
25
+ // [START gae_handle_error]
25
26
if ( response . status === 401 ) {
26
27
statusElm . innerHTML = 'Login stale. <input type="button" value="Refresh" onclick="sessionRefreshClicked();"/>' ;
27
28
}
29
+ // [END gae_handle_error]
28
30
// [END handle_error]
29
31
else {
30
32
statusElm . innerHTML = response . statusText ;
@@ -42,6 +44,7 @@ getStatus();
42
44
setInterval ( getStatus , 10000 ) ; // 10 seconds
43
45
44
46
// [START refresh_session]
47
+ // [START gae_refresh_session]
45
48
var iapSessionRefreshWindow = null ;
46
49
47
50
function sessionRefreshClicked ( ) {
@@ -54,16 +57,29 @@ function sessionRefreshClicked() {
54
57
55
58
function checkSessionRefresh ( ) {
56
59
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
58
70
if ( response . status === 401 ) {
71
+ // No new session detected. Try to get a session again
59
72
window . setTimeout ( checkSessionRefresh , 500 ) ;
60
73
} else {
74
+ // Session retrieved.
61
75
iapSessionRefreshWindow . close ( ) ;
62
76
iapSessionRefreshWindow = null ;
63
77
}
78
+ } )
64
79
} ) ;
65
80
} else {
66
81
iapSessionRefreshWindow = null ;
67
82
}
68
83
}
84
+ // [END gae_refresh_session]
69
85
// [END refresh_session]
0 commit comments