From eb795d4e336d637bb17d3f2b949708c6e94367e3 Mon Sep 17 00:00:00 2001 From: Alex Phillips Date: Mon, 7 Feb 2022 19:16:47 -0500 Subject: [PATCH] added ping to help preserve jwt sessions --- frontend/src/App.js | 6 ++++++ frontend/src/api.js | 2 +- frontend/src/utils/useInterval.js | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 frontend/src/utils/useInterval.js diff --git a/frontend/src/App.js b/frontend/src/App.js index fa94224..5e606be 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -10,10 +10,16 @@ import Login from './components/Login' import { useDispatch, useSelector } from 'react-redux' import AddAccountDialog from './components/AddAccountDialog' import { ThemeProvider, createTheme } from '@mui/material/styles' +import useInterval from './utils/useInterval' +import API from './api' export default function App(props) { const theme = useSelector(state => state.app.theme) + useInterval(() => { + API.ping() + }, 1800000) + const darkTheme = createTheme({ palette: { mode: 'dark', diff --git a/frontend/src/api.js b/frontend/src/api.js index 949baf6..38f2b75 100644 --- a/frontend/src/api.js +++ b/frontend/src/api.js @@ -7,7 +7,7 @@ const axios = Axios.create({ export default class API { static async ping() { - const response = await axios.get('/api/me') + const response = await axios.post('/api/ping') return response.data.data } diff --git a/frontend/src/utils/useInterval.js b/frontend/src/utils/useInterval.js new file mode 100644 index 0000000..4d80dd9 --- /dev/null +++ b/frontend/src/utils/useInterval.js @@ -0,0 +1,22 @@ +import { useEffect, useRef } from 'react' + +export default function (callback, delay) { + const savedCallback = useRef() + // Remember the last callbac + useEffect(() => { + savedCallback.current = callback + }, [callback]) + + // Set up the interval + useEffect(() => { + function tick() { + savedCallback.current() + } + if (delay !== null) { + const id = setInterval(tick, delay) + return () => { + clearInterval(id) + } + } + }, [callback, delay]) +}