|
| 1 | +// Copyright Google Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +"use strict"; |
| 16 | + |
| 17 | +function getStatus() { |
| 18 | + var statusElm = document.getElementById('status'); |
| 19 | + statusElm.innerHTML = 'Polling'; |
| 20 | + fetch('/status').then(function(response) { |
| 21 | + if (response.ok) { |
| 22 | + return response.text(); |
| 23 | + } |
| 24 | + // [START handle_error] |
| 25 | + if (response.status === 401) { |
| 26 | + statusElm.innerHTML = 'Login stale. <input type="button" value="Refresh" onclick="sessionRefreshClicked();"/>'; |
| 27 | + } |
| 28 | + // [END handle_error] |
| 29 | + else { |
| 30 | + statusElm.innerHTML = response.statusText; |
| 31 | + } |
| 32 | + throw new Error (response.statusText); |
| 33 | + }) |
| 34 | + .then(function(text) { |
| 35 | + statusElm.innerHTML = text; |
| 36 | + }) |
| 37 | + .catch(function(statusText) { |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +getStatus(); |
| 42 | +setInterval(getStatus, 10000); // 10 seconds |
| 43 | + |
| 44 | +// [START refresh_session] |
| 45 | +var iapSessionRefreshWindow = null; |
| 46 | + |
| 47 | +function sessionRefreshClicked() { |
| 48 | + if (iapSessionRefreshWindow == null) { |
| 49 | + iapSessionRefreshWindow = window.open("/_gcp_iap/do_session_refresh"); |
| 50 | + window.setTimeout(checkSessionRefresh, 500); |
| 51 | + } |
| 52 | + return false; |
| 53 | +} |
| 54 | + |
| 55 | +function checkSessionRefresh() { |
| 56 | + if (iapSessionRefreshWindow != null && !iapSessionRefreshWindow.closed) { |
| 57 | + fetch('/favicon.ico').then(function(response) { |
| 58 | + if (response.status === 401) { |
| 59 | + window.setTimeout(checkSessionRefresh, 500); |
| 60 | + } else { |
| 61 | + iapSessionRefreshWindow.close(); |
| 62 | + iapSessionRefreshWindow = null; |
| 63 | + } |
| 64 | + }); |
| 65 | + } else { |
| 66 | + iapSessionRefreshWindow = null; |
| 67 | + } |
| 68 | +} |
| 69 | +// [END refresh_session] |
0 commit comments