Skip to content

Commit 7f71c80

Browse files
colinln-lark
andauthored
Handle redirection to login page if session has expired when websocket connection is attempted (#2078)
Co-authored-by: Noley Holland <63617269+n-lark@users.noreply.github.com>
1 parent c8ea9dc commit 7f71c80

1 file changed

Lines changed: 46 additions & 17 deletions

File tree

ui/src/main.mjs

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,52 @@ fetch('_setup')
225225
// default interval - every 2.5 seconds
226226
function reconnect (interval = 2500) {
227227
if (disconnected) {
228-
socket.connect()
229-
if (retryCount >= 14) {
230-
// trying for over 1 minute
231-
interval = 30000 // interval at 30 seconds
232-
} else if (retryCount >= 4) {
233-
// trying for over 10 seconds
234-
interval = 5000 // interval at 5 seconds
235-
}
236-
retryCount++
237-
// if still within our maximum retry count
238-
if (retryCount <= MAX_RETRIES) {
239-
// check for a connection again in <interval> milliseconds
240-
reconnectTO = setTimeout(reconnect, interval)
241-
} else {
242-
// we have been retrying for 5 minutes so give up and reload the page
243-
forcePageReload('Too many retries')
244-
}
228+
/** Prior to trying the socket connect, use an http fetch to check for redirection to auth proxy
229+
* fetch '_setup' as that is a short json file
230+
* use redirect: 'manual' to stop any redirection to a login page in case it causes a CORS error
231+
*/
232+
fetch('_setup', { redirect: 'manual', cache: 'no-cache' })
233+
.then(function (res) {
234+
const contentType = res.headers?.get('content-type')
235+
/** If the content type is not application/json then likely it is a login request,
236+
* or a failed redirection to login page, either of which would cause the websocket
237+
* connect to fail, so reload the page in order to show the login request.
238+
* Allow it continue the first few times though, allowing the socket connect code to fail
239+
* and retry, in case this is just a transient issue
240+
* retryCount will still be 0 after first failure, so it will not force a page reload until
241+
* it has failed 3 times
242+
*/
243+
if ((contentType && contentType.includes('application/json')) || retryCount < 2) {
244+
tryConnect(interval)
245+
} else {
246+
forcePageReload('Websocket pre-fetch failed')
247+
}
248+
})
249+
.catch(function () {
250+
// there is some sort of network failure, let the websocket connection code handle that
251+
tryConnect(interval)
252+
})
253+
}
254+
}
255+
256+
// default interval - every 2.5 seconds
257+
function tryConnect (interval) {
258+
socket.connect()
259+
if (retryCount >= 14) {
260+
// trying for over 1 minute
261+
interval = 30000 // interval at 30 seconds
262+
} else if (retryCount >= 4) {
263+
// trying for over 10 seconds
264+
interval = 5000 // interval at 5 seconds
265+
}
266+
retryCount++
267+
// if still within our maximum retry count
268+
if (retryCount <= MAX_RETRIES) {
269+
// check for a connection again in <interval> milliseconds
270+
reconnectTO = setTimeout(reconnect, interval)
271+
} else {
272+
// we have been retrying for 5 minutes so give up and reload the page
273+
forcePageReload('Too many retries')
245274
}
246275
}
247276

0 commit comments

Comments
 (0)