Skip to content

Commit

Permalink
feat: start session refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
Naomi Aro committed Oct 12, 2021
1 parent eadf23b commit 8ff0cf3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/components/SessionRefresher.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React, {useEffect} from 'react';
import throttle from 'lodash.throttle';

interface Props {
refreshUrl: string;
}

const SessionRefresher: React.FunctionComponent<Props> = ({refreshUrl}) => {
const extendSession = async () => {
try {
await fetch(refreshUrl);
} catch (e) {
console.error(e);
}
};

useEffect(() => {
const throttledSession = throttle(extendSession, 1000 * 60);
window.addEventListener('keydown', throttledSession);
window.addEventListener('mousedown', throttledSession);
window.addEventListener('scroll', throttledSession);

return () => {
window.removeEventListener('keydown', throttledSession);
window.removeEventListener('mousedown', throttledSession);
window.removeEventListener('scroll', throttledSession);
throttledSession.cancel();
};
}, []);

return null;
};

export default SessionRefresher;
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"json-schema": "^0.2.5",
"keycloak-connect": "^10.0.0",
"lightship": "^6.7.2",
"lodash.throttle": "^4.1.1",
"moment-timezone": "^0.5.33",
"morgan": "^1.10.0",
"next": "^10.2.3",
Expand Down
5 changes: 5 additions & 0 deletions app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9027,6 +9027,11 @@ lodash.truncate@^4.4.2:
resolved "https://registry.yarnpkg.com/lodash.truncate/-/lodash.truncate-4.4.2.tgz#5a350da0b1113b837ecfffd5812cbe58d6eae193"
integrity sha1-WjUNoLERO4N+z//VgSy+WNbq4ZM=

lodash.throttle@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.throttle/-/lodash.throttle-4.1.1.tgz#c23e91b710242ac70c37f1e1cda9274cc39bf2f4"
integrity sha1-wj6RtxAkKscMN/HhzaknTMOb8vQ=

lodash.union@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.union/-/lodash.union-4.6.0.tgz#48bb5088409f16f1821666641c44dd1aaae3cd88"
Expand Down

0 comments on commit 8ff0cf3

Please sign in to comment.