@sidneyroberto9/sentret-react
v1.0.0
Published
Browser client for the sentret inactivity hub: heartbeat, status poll, expiry warning and cross-tab sync, with React bindings.
Maintainers
Readme
@sidneyroberto9/sentret-react
Browser client for the sentret inactivity hub served by
@sidneyroberto9/sentret-fastify (or by the Java
sentret-session, same wire contract).
It sends a throttled heartbeat on user activity, polls the session status, shows an expiry
warning with a countdown, and keeps every tab in sync. The session cookie is HttpOnly: the
client never reads it, it only calls the hub with credentials: 'include'.
Install
npm install @sidneyroberto9/sentret-reactRequires React 18+.
Usage (Vite)
import { SentretProvider } from '@sidneyroberto9/sentret-react'
createRoot(document.getElementById('root')!).render(
<SentretProvider
hubUrl={import.meta.env.VITE_API_URL + '/session'}
appLogoutUrl={import.meta.env.VITE_API_URL + '/auth/logout'}
>
<App />
</SentretProvider>,
)import { useSentret } from '@sidneyroberto9/sentret-react'
function LoginForm() {
const { refresh } = useSentret()
async function onSubmit(credentials: { email: string; password: string }) {
await fetch(import.meta.env.VITE_API_URL + '/auth/login', {
method: 'POST',
credentials: 'include',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(credentials),
})
await refresh() // the provider flips to authenticated now, no waiting for the poll
}
// ...
}
function Header() {
const { user, authenticated, logout } = useSentret()
if (!authenticated) return null
return <button onClick={() => void logout()}>Sign out {user?.email}</button>
}useSentret() returns { status, user, authenticated, warning, renew, logout, refresh } and throws
outside a <SentretProvider>. user is { userId, email? } | null.
Every API call (fetch or axios) must send the cookie: credentials: 'include' for fetch,
withCredentials: true for axios. Your API also needs CORS with credentials: true for your origin.
Provider props
| Prop | Description |
|---|---|
| hubUrl | Full hub base URL, e.g. https://api.example.com/session. /status, /heartbeat and /renew are appended; trailing slashes are stripped. |
| appLogoutUrl | Your own logout endpoint. POSTed (with credentials) before redirecting when the session ends. |
| renderWarning | Replaces the warning card. Receives { remainingMs, remainingSeconds, formattedRemaining, cause, renew }. |
| labels | Overrides the default card copy (title, message, continueLabel; pt-BR by default). |
<SentretProvider
hubUrl={url}
labels={{ title: 'Session expiring', message: 'You will be signed out in', continueLabel: 'Stay signed in' }}
/>
<SentretProvider
hubUrl={url}
renderWarning={({ formattedRemaining, renew }) => (
<Dialog>
Signing out in {formattedRemaining} <button onClick={renew}>Stay</button>
</Dialog>
)}
/>Behaviour
- Timings (
heartbeatIntervalMs,statusPollIntervalMs,warningBeforeMs) andloginUrlcome from the hub; nothing is hardcoded in the client. - The heartbeat fires on mouse, keyboard, touch and scroll activity, leading-edge throttled.
- The status poll is the authority. When the hub reports the user is no longer authenticated (after
having been authenticated), the client terminates once: broadcasts to other tabs, POSTs
appLogoutUrl, redirects tologinUrlif the hub has one. An anonymous first status never redirects, so the login page does not loop. - While the warning is shown, activity does not heartbeat: only
renew()dismisses it. - Tabs sync through a
BroadcastChannelnamedsentret-session(logout and renew propagate). - After
logout()(or an expiry) the client is finished. Remount the provider, or let the redirect tologinUrlreload the page, before logging in again.
Core without React
SentretClient / createSentretClient({ hubUrl, appLogoutUrl, listeners }) is exported for other
frameworks: start(), stop(), renew(), logout(), refresh(), notifyActivity().
License
MIT
