variapSessionRefreshWindow=null;functionsessionRefreshClicked(){if(iapSessionRefreshWindow==null){iapSessionRefreshWindow=window.open("/?gcp-iap-mode=DO_SESSION_REFRESH");window.setTimeout(checkSessionRefresh,500);}returnfalse;}functioncheckSessionRefresh(){if(iapSessionRefreshWindow!=null && !iapSessionRefreshWindow.closed){// Attempting to start a new session.// XMLHttpRequests is used by the server to identify AJAX requestsfetch('/favicon.ico',{method:"GET",credentials:'include',headers:{'X-Requested-With':'XMLHttpRequest'}.then((response)=>{// Checking if browser has a session for the requested appif(response.status===401){// No new session detected. Try to get a session againwindow.setTimeout(checkSessionRefresh,500);}else{// Session retrieved.iapSessionRefreshWindow.close();iapSessionRefreshWindow=null;}})});}else{iapSessionRefreshWindow=null;}}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003eThis article outlines how to manage Identity-Aware Proxy (IAP) sessions when using external identities, highlighting that sessions are valid for one hour and require reauthentication upon expiration.\u003c/p\u003e\n"],["\u003cp\u003eNon-AJAX requests are automatically handled via application redirect, while AJAX requests require specific handling due to the phasing out of third-party cookies in modern browsers.\u003c/p\u003e\n"],["\u003cp\u003eWhen an AJAX request receives a \u003ccode\u003e401: Unauthorized\u003c/code\u003e status code due to an expired token, it can be handled by modifying the application code, adding an \u003ccode\u003eiframe\u003c/code\u003e for session refresh, or directing users to manually refresh the session in a separate tab.\u003c/p\u003e\n"],["\u003cp\u003eTo sign out a user from an IAP resource, use the \u003ccode\u003e?gcp-iap-mode=GCIP_SIGNOUT\u003c/code\u003e query parameter, or to sign out from all resources and sessions, redirect them to the authentication URL with \u003ccode\u003eapiKey\u003c/code\u003e and \u003ccode\u003emode=signout\u003c/code\u003e parameters.\u003c/p\u003e\n"],["\u003cp\u003eTo restart the tenant selection process for a user who wants to switch between multiple tenants, use the \u003ccode\u003e?gcp-iap-mode=CLEAR_LOGIN_COOKIE\u003c/code\u003e query parameter in the URL.\u003c/p\u003e\n"]]],[],null,["# Managing sessions with external identities\n\nThis article explains how to manage sessions with Identity-Aware Proxy\n(IAP) if you're using external identities for authentication.\n\nRefreshing sessions\n-------------------\n\nIdentity Platform sessions are valid for one hour. When a session expires,\nyour app needs to redirect to the authentication page. The authentication\npage contains the Identity Platform refresh token. As long as the user's\ncredential is still valid, you can use it for reauthentication without showing\nany UI.\n\nIf the user recently changed their email or password, or some other action\nthat revoked their token occurred, they'll need to complete the authentication\nflow again.\n\n### Handling non-AJAX requests\n\nNon-AJAX requests are handled automatically using an application redirect,\nassuming the authentication page is configured correctly.\n\n### Handling AJAX requests\n\nChrome and other browsers are [phasing out](https://blog.chromium.org/2020/01/building-more-private-web-path-towards.html)\nthird-party cookies. The recommendations for making AJAX requests in this page\nwon't work if [third-party cookies](https://developers.google.com/privacy-sandbox/3pcd/prepare/audit-cookies#understand)\nare disabled. However, the provided recommendations will remain functional if\nboth the source and target of the AJAX [requests](https://web.dev/articles/url-parts) are from the [same site](https://web.dev/articles/same-site-same-origin#same-site-cross-site).\n\nFor instructions on managing third-party cookies in Chrome, see [Delete, allow\nand manage cookies in Chrome](https://support.google.com/chrome/answer/95647?sjid=7241780428986433770-NC).\n\nIf you send an AJAX request with an expired token, the request will return a\n`401: Unauthorized` status code. Implement one of the following solutions to\nhandle this:\n\n- Modify your application code to handle HTTP `401` status codes.\n- Add an `iframe` to your application to point to the session refresher.\n- Instruct your users to manually load the session refresher in a separate tab.\n\nIf you're receiving a `302` status code instead of `401` in response to AJAX\nrequests, add an `X-Requested-With` header with a value of `XMLHttpRequest`.\nThis informs IAP that the request originates from JavaScript.\n\n#### Programmatically handling HTTP 401\n\nProgrammatically handling HTTP `401` status codes is the recommended way to\nrefresh an AJAX session. To do this:\n\n1. Update your application code to handle the error.\n\n\n if (response.status === 401) {\n statusElm.innerHTML = 'Login stale. \u003cinput type=\"button\" value=\"Refresh\" onclick=\"sessionRefreshClicked();\"/\u003e';\n }\n\n \u003cbr /\u003e\n\n2. Add a handler that opens a window to reauthenticate the user, then closes\n it when the process completes.\n\n\n var iapSessionRefreshWindow = null;\n\n function sessionRefreshClicked() {\n if (iapSessionRefreshWindow == null) {\n iapSessionRefreshWindow = window.open(\"/?gcp-iap-mode=DO_SESSION_REFRESH\");\n window.setTimeout(checkSessionRefresh, 500);\n }\n return false;\n }\n\n function checkSessionRefresh() {\n if (iapSessionRefreshWindow != null && !iapSessionRefreshWindow.closed) {\n // Attempting to start a new session.\n // XMLHttpRequests is used by the server to identify AJAX requests\n fetch('/favicon.ico', {\n method: \"GET\",\n credentials: 'include',\n headers: {\n 'X-Requested-With': 'XMLHttpRequest'\n }\n .then((response) =\u003e {\n // Checking if browser has a session for the requested app\n if (response.status === 401) {\n // No new session detected. Try to get a session again\n window.setTimeout(checkSessionRefresh, 500);\n } else {\n // Session retrieved.\n iapSessionRefreshWindow.close();\n iapSessionRefreshWindow = null;\n }\n })\n });\n } else {\n iapSessionRefreshWindow = null;\n }\n }\n\n \u003cbr /\u003e\n\n#### Using an iframe\n\nIf you aren't able to handle HTTP `401` programmatically, the next best solution\nis to add an `iframe` to your application that points to the session refresher.\n\nUsing an iframe requires you to configure a custom sign-in page on the same\ndomain as the IAP-secured web app. Otherwise,\nusers will encounter cross-origin errors. For more information on sign-in page configuration, see\n[creating a custom sign-in page](/iap/docs/create-custom-auth-ui).\n\nExample usage of an iframe: \n\n \u003ciframe src=\"https://example.com/some/path?gcp-iap-mode=SESSION_REFRESHER\" style=\"width:0;height:0;border:0; border:none;\"\u003e\u003c/iframe\u003e\n\n#### Loading the session refresher\n\nAs a last resort, you can instruct your users to\nmanually load the session refresher. Add guidance to your application or its\ndocumentation directing users to open the following URL in a separate\ntab: \n\n```\nhttps://example.com/some/path?gcp-iap-mode=SESSION_REFRESHER\n```\n\nSigning users out\n-----------------\n\nTo sign out a user from a IAP resource, use the query parameter\n`?gcp-iap-mode=GCIP_SIGNOUT`. For example, in an App Engine app, the\nURL looks like this: \n\n```\nhttps://example.com/some/path?gcp-iap-mode=GCIP_SIGNOUT\n```\n\nUsers will be redirected back to the sign-in page after they're logged out.\n\nTo sign out a user from *all* resources and sessions, redirect them to your\nauthentication URL with your API key and `mode=signout` appended as parameters. For example: \n\n```\nhttps://auth.example.com/?apiKey=API-KEY&mode=signout\n```\n\nUsers will remain on the page after sign-out completes. Consider implementing\nthe `completeSignOut()` callback on the `AuthenticationHandler` object to\nprovide feedback to the user that they've signed out successfully.\n\nSwitching between tenants\n-------------------------\n\nIn some cases, a user might want to authenticate with several tenants for the\nsame IAP resource. For example, they might belong\nto multiple tenants that grant different levels of access, and want to change to\na tenant with fewer or greater privileges.\n\nTo force the tenant selection process to restart, use\n`?gcp-iap-mode=CLEAR_LOGIN_COOKIE`. For example, in a App Engine app,\nthe URL might look like this: \n\n```\nhttps://PROJECT-ID.appspot.com/some/path?gcp-iap-mode=CLEAR_LOGIN_COOKIE\n```\n\nWhat's next\n-----------\n\n- [Create an authentication UI with FirebaseUI](/iap/docs/using-firebaseui)\n- [Create a custom authentication UI](/iap/docs/create-custom-auth-ui)"]]