Skip to main content
Login

Sign in to access your account

Session Timeout - National Design System

A warning that opens before an idle session ends, counts down the time left, and lets the user stay signed in

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:

  • the page shows a deadline, such as the day applications close: Countdown
  • the user must confirm an action they started: Modal

Markup

// Once on every signed-in page. The values come from your server. NDS.SessionTimeout.init({ timeout: 900, warn: 120, extend: '/session/keepalive', logout: '/logout' });

Behavior

Session Renewal

Each extension must renew the session on the server. Use one of these two ways:

  • The extend option. The script sends a POST to that URL. A 401 or 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 answers 403.
  • Your own request. Leave out extend and send the request in the nds:session:extend event. Use it when your server needs a CSRF token or other headers. When the server says the session is gone, call NDS.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 warn under 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 warn at 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 and destroy() 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-store header. 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. Use sessionStorage, not localStorage: on a shared computer, localStorage keeps the data after the user leaves.
  • When two services with their own sign-in share one site, such as /service-a and /service-b, give each its own key.

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 keep-alive needs a CSRF token: extend is left out, and the page renews here. document.addEventListener('nds:session:extend', async () => { const res = await fetch('/keepalive', { method: 'POST', headers: { 'X-CSRF-Token': csrfToken }, redirect: 'manual' }); // A 401 or a redirect to the sign-in page: the session is gone. if (res.status === 401 || res.type === 'opaqueredirect') NDS.SessionTimeout.end(); }); // The autosave request renewed the session on the server. autosave().then(() => NDS.SessionTimeout.reset()); // Keep the draft in this tab, so it is there after the user signs in again. document.addEventListener('nds:session:end', () => { sessionStorage.setItem('application-draft', JSON.stringify(readForm())); });

The full API is in the banner of _js/nds-session-timeout.js.

Last Modified Date: 31/10/2026 - 08:31 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks