added ping to help preserve jwt sessions

This commit is contained in:
Alex Phillips 2022-02-07 19:16:47 -05:00
parent 6ccf4d2f28
commit eb795d4e33
3 changed files with 29 additions and 1 deletions

View File

@ -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',

View File

@ -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
}

View File

@ -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])
}