BACK
Use casesIdle auto-logout — logging out on inactivity
Use cases · 09

Idle auto-logout — logging out on inactivity

Pattern

In admin panels, banking portals, and editors, a session should end after a period of inactivity. In vanilla JS this becomes manual juggling of setTimeout; in RxJS it's a single pipeline.

What we solve

  • Merging sources. Clicks, input, scroll — all of it is "activity".
  • Resetting the timer. Every event needs to restart the countdown.
  • One logout signal. At the end we emit exactly one event for the component to clear the session on.

The operators and their roles

  • merge(click$, key$, ...) — gather all activity sources into one stream.
  • switchMap(() => timer(N)) — restart the countdown on each event, cancelling the previous one.
  • mapTo('logout') — turn the timer firing into a meaningful event.

What we achieve

A clean declaration: "logout = silence longer than N". No counters, no manual clearTimeout.

script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...