Trying to build an auto-logout feature, and have got a timer going but looking for a way to start/restart the timer based on user's last active time.
The scenario is that the user has been away from their keyboard for X amount of time, and we want to log them out.
Adding the code I've come up with since then:
// main.js
import React from 'react'
import { Admin } from 'react-admin'
import authProvider from '../../auth-provider'
import restClient from '../../rest-client'
import buildResources from './build-resources'
import { setLastActive } from './last-active'
const customReducers = {
lastActive,
}
function lastActive() {
const now = Date.now()
setLastActive(now)
return now
}
export const Main = () => (
<Admin
authProvider={authProvider}
dataProvider={restClient}
customReducers={customReducers}
>
{buildResources}
</Admin>
)
// last-active.js
let lastActive
export const setLastActive = (timestamp = Date.now()) =>
(lastActive = timestamp)
export const getLastActive = () => lastActive || null
It's not all the code, but enough to show the gist. I can know use the redux store, or the singleton to grab when they were last active. It's a bit flawed since most, not all actions go through the redux store.