diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-01-09 14:13:48 +0100 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-02-06 14:15:47 +0100 |
commit | d1886b57ced17b077c1368be169f9a9ce731ae80 (patch) | |
tree | 42b547be561f3417cebeb3cf57ef117794a4266d /core/src | |
parent | daf6887c09b3b706728c5fdef6cb6df0640f1e21 (diff) | |
download | nextcloud-server-d1886b57ced17b077c1368be169f9a9ce731ae80.tar.gz nextcloud-server-d1886b57ced17b077c1368be169f9a9ce731ae80.zip |
Fetch a new request token as soon as the browser becomes online
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'core/src')
-rw-r--r-- | core/src/session-heartbeat.js | 64 |
1 files changed, 58 insertions, 6 deletions
diff --git a/core/src/session-heartbeat.js b/core/src/session-heartbeat.js index 49cf547aa36..88c519e1dde 100644 --- a/core/src/session-heartbeat.js +++ b/core/src/session-heartbeat.js @@ -20,6 +20,7 @@ */ import $ from 'jquery' +import { emit } from '@nextcloud/event-bus' import { generateUrl } from './OC/routing' import OC from './OC' @@ -54,6 +55,34 @@ const getInterval = () => { ) } +const getToken = async() => { + const url = generateUrl('/csrftoken') + + // Not using Axios here as Axios is not stubbable with the sinon fake server + // see https://stackoverflow.com/questions/41516044/sinon-mocha-test-with-async-ajax-calls-didnt-return-promises + // see js/tests/specs/coreSpec.js for the tests + const resp = await $.get(url) + + return resp.token +} + +const poll = async() => { + try { + const token = await getToken() + setRequestToken(token) + } catch (e) { + console.error('session heartbeat failed', e) + } +} + +const startPolling = () => { + const interval = setInterval(poll, getInterval() * 1000) + + console.info('session heartbeat polling started') + + return interval +} + /** * Calls the server periodically to ensure that session and CSRF * token doesn't expire @@ -63,12 +92,35 @@ export const initSessionHeartBeat = () => { console.info('session heartbeat disabled') return } + let interval = startPolling() + + window.addEventListener('online', async() => { + console.info('browser is online again, resuming heartbeat') + interval = startPolling() + try { + await poll() + console.info('session token successfully updated after resuming network') - setInterval(() => { - $.ajax(generateUrl('/csrftoken')) - .then(resp => setRequestToken(resp.token)) - .fail(e => { - console.error('session heartbeat failed', e) + // Let apps know we're online and requests will have the new token + emit('networkOnline', { + success: true }) - }, getInterval() * 1000) + } catch (e) { + console.error('could not update session token after resuming network', e) + + // Let apps know we're online but requests might have an outdated token + emit('networkOnline', { + success: false + }) + } + }) + window.addEventListener('offline', () => { + console.info('browser is offline, stopping heartbeat') + + // Let apps know we're offline + emit('networkOffline', {}) + + clearInterval(interval) + console.info('session heartbeat polling stopped') + }) } |