Overview
Session Timeout warns a signed-in user before their session ends with no activity, and lets them extend it. WCAG 2.2.1, Timing Adjustable, asks for this warning. You add no markup. One NDS.SessionTimeout.init() call times the session. The script builds a warning Modal with a Countdown in it, and the ended modal.
Connect it to your server in one of two ways, as Session Renewal shows.
Pick another component when:
Markup
Behavior
Session Renewal
Each extension must renew the session on the server. Use one of these two ways:
- The
extendoption. The script sends a POST to that URL. A401or a redirect to a sign-in page ends the session at once. Any other failure keeps the old deadline, so the warning comes back in time. The request carries cookies but no CSRF token, so a server that asks for one answers403. - Your own request. Leave out
extendand send the request in thends:session:extendevent. Use it when your server needs a CSRF token or other headers. When the server says the session is gone, callNDS.SessionTimeout.end().
Timing
The session ends timeout seconds after its last renewal, the way the server counts it. The warning opens warn seconds before the end, and its countdown starts at the time left. A user who closes the warning in any way extends the session.
The clock starts when init() runs, because the request that served the page renewed the session. If your server renews it only on some requests, pass the seconds left as left.
Session End
At the end, the script fires nds:session:end. With logout, the page then goes to that URL. Without it, the ended modal opens. Call preventDefault() on the event to do neither. The warning then closes, and the next step is yours. If the user goes back to the page after the redirect, the script goes to the logout URL again.
A server that signs out only on a POST, such as one that needs a CSRF token, gets no logout. Call preventDefault() on nds:session:end and send the POST there. Without logout, the warning shows no Sign out link.
Built-in Features
-
Built Modals
The script builds the warning and the ended modal when each first opens, with the text of the page's language.
-
Activity Renewal
A click, a tap, a key or the mouse wheel renews the session while the warning is closed. It renews at most once a minute, or more often in a short session. A user who keeps working never sees the warning.
-
Shared Across Tabs
Tabs of the same service share one deadline in
localStorage. Work in one tab keeps every tab signed in, and a renewal in one tab closes the warning in the others. -
Focus Without a Ring
The timer opens the warning, not the user, so focus goes to the modal itself and no focus ring shows. Tab shows the ring, and Enter presses Stay signed in.
-
Sleep and Return
The script checks the deadline again when a hidden tab shows, when the computer wakes, and when the browser restores the page from its history. A session that ended meanwhile ends at once.
-
WCAG Time Check
A
warnunder 20 seconds logs a console warning. WCAG 2.2.1 gives the user at least 20 seconds to extend.
Best Practices
- Pass the server’s idle timeout as
timeout, from the server. A wrong value warns too late or too early. - Give the user time to act. Keep
warnat 120 seconds or more on a page with a long form. - Call
init()once on every signed-in page, such as in the script of the shared layout. Never call it on a public page. - In a single-page app, call
init()after sign-in anddestroy()at sign-out. - Call
NDS.SessionTimeout.reset()after a request of your own renews the session, such as an autosave. In a single-page app, call it from the response hook of your HTTP client. - On a page with personal data, send the
Cache-Control: no-storeheader. The browser then does not keep the page, so the Back button cannot show it again after the session ends. - Save the user’s unsaved form data in
nds:session:end, so it is there after they sign in again. UsesessionStorage, notlocalStorage: on a shared computer,localStoragekeeps the data after the user leaves. - When two services with their own sign-in share one site, such as
/service-aand/service-b, give each its ownkey.
API
Styling
Both modals carry .nds-session-timeout beside their Modal classes. Put it in your own selectors to style these two modals and no other.
Text
The modals read their English from the script and their Arabic from the session-timeout section of assets/i18n/ar.json. To change some of it, set window.NDS_I18N['session-timeout'] before the NDS scripts: each key you set replaces that text, and the rest keep the pack’s text. See Internationalization. On an Arabic page, the countdown still reads 02:03 left to right, in Latin digits.
JavaScript
The NDS.SessionTimeout script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
| Method | Effect |
|---|---|
NDS.SessionTimeout.init(options) |
Starts timing the session |
NDS.SessionTimeout.extend() |
Extends the session now: it fires nds:session:extend and sends the keep-alive request |
NDS.SessionTimeout.reset() |
Starts the deadline again from now, with no event and no request. Call it after a request of your own renews the session. It closes an open warning, and it restarts an ended session, such as after the user signs in again on the same page |
NDS.SessionTimeout.end() |
Ends the session now. Call it when your own request finds the session gone |
NDS.SessionTimeout.destroy() |
Stops the timers and listeners, and removes both session modals. The next init() builds them again |
| Option | Default | Effect |
|---|---|---|
timeout |
— | Required. The server’s idle timeout, in seconds. It must be above warn, or the script logs a console warning and does not start |
warn |
120 |
Seconds before the end at which the warning opens |
left |
timeout |
Seconds left in the session when the server rendered the page |
extend |
— | A URL the script sends a POST to each time the session is extended. It sends cookies only to a URL on the same origin |
logout |
— | A URL the page goes to at the end. The warning shows a Sign out link to it only with this option |
signin |
this page | The URL of the Sign in link in the ended modal. With the default, your server sends the signed-out user from this page to sign in |
key |
the extend URL |
The name under which tabs share the deadline in localStorage. Without extend, every page on the same origin shares one name |
| Event | Fired on | Detail |
|---|---|---|
nds:session:extend |
document, each time the session is extended |
{ reason }: activity, confirm (the user closed the warning) or api (extend()) |
nds:session:end |
document, once, at the end |
None. Cancelable: preventDefault() stops the redirect and the ended modal |
The full API is in the banner of _js/nds-session-timeout.js.