WEB-608: feat: enhance OAuth redirect interceptor with error handling and path validation
add try and catch
This commit is contained in:
committed by
Adam Saghy
parent
9de0cb0216
commit
5b6d0d6679
+51
-9
@@ -31,16 +31,58 @@
|
|||||||
<!-- Load environment variables -->
|
<!-- Load environment variables -->
|
||||||
</head>
|
</head>
|
||||||
<body class="h-full">
|
<body class="h-full">
|
||||||
|
<!--
|
||||||
|
OAuth Redirect Interceptor (PRODUCTION)
|
||||||
|
-------------------------------------
|
||||||
|
OAuth providers cannot redirect to hash-based routes (#).
|
||||||
|
This script intercepts OAuth callbacks BEFORE Angular initializes,
|
||||||
|
stores the response parameters, and rewrites the URL to a hash route
|
||||||
|
that Angular can handle.
|
||||||
|
|
||||||
|
DO NOT remove unless migrating to path-based routing.
|
||||||
|
-->
|
||||||
<script>
|
<script>
|
||||||
function getQueryParam(param) {
|
(function () {
|
||||||
const urlParams = new URLSearchParams(window.location.search);
|
// ===== CONFIGURATION =====
|
||||||
return urlParams.get(param);
|
var OAUTH_CALLBACK_PATHS = ['/callback'];
|
||||||
}
|
var STORAGE_KEY = 'oauth_callback_query';
|
||||||
const code = getQueryParam('code');
|
|
||||||
if (code) {
|
// ===== DETECTION =====
|
||||||
sessionStorage.setItem('oauth_callback_query', window.location.search);
|
var params = new URLSearchParams(window.location.search);
|
||||||
window.location.href = '/#/callback';
|
var isOAuthResponse = params.has('code') || params.has('error');
|
||||||
}
|
|
||||||
|
if (!isOAuthResponse) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prevent redirect loop
|
||||||
|
if (window.location.hash && window.location.hash.length > 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var currentPath = window.location.pathname;
|
||||||
|
|
||||||
|
// Only allow known OAuth callback paths
|
||||||
|
if (OAUTH_CALLBACK_PATHS.indexOf(currentPath) === -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===== PERSIST & REDIRECT =====
|
||||||
|
try {
|
||||||
|
sessionStorage.setItem(STORAGE_KEY, window.location.search);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('OAuth interceptor: sessionStorage unavailable, appending query to hash');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always redirect, even if storage fails
|
||||||
|
var redirectUrl = window.location.origin + '/#' + currentPath;
|
||||||
|
|
||||||
|
if (!sessionStorage.getItem(STORAGE_KEY)) {
|
||||||
|
redirectUrl += window.location.search;
|
||||||
|
}
|
||||||
|
|
||||||
|
window.location.replace(redirectUrl);
|
||||||
|
})();
|
||||||
</script>
|
</script>
|
||||||
<script src="assets/env.js"></script>
|
<script src="assets/env.js"></script>
|
||||||
<!--[if lt IE 10]>
|
<!--[if lt IE 10]>
|
||||||
|
|||||||
Reference in New Issue
Block a user