Automatic reconnection

A 4D web session dies with a 4D restart or when its own timeout expires. So that users do not have to type their password again, BWEB reconnects them silently with a reconnection cookie, registerClient, handled by the WebReconnectToken class.

The duration is set per domain in /bweb/domain, and it slides: every reconnection replaces the token with a new one, valid for the full duration.

Login, logout and rights themselves are covered in Users and rights.

The principle: an opaque cookie

The cookie value is <uuidKey of the account>.<token>, the token being a random string of 64 hex characters. The server stores only the SHA-256 hash of the token, in an object field of the account:

BSPK_USER.bspkReconnect = { vc_Tokens: [ {sha; ip; authAt; expiresAt; rotated?}, … ] }
  • ip: the browser IP at issue time;
  • authAt: the time (in seconds) of the password entry this token descends from;
  • expiresAt: the expiry time; expired entries are dropped at every read;
  • at most maxDevices entries (10 by default), one per device: the oldest go first.

The domain settings

In /bweb/domain, the "Reconnexion automatique" card writes three keys into BSPK_WEB_DOMAIN.params, read by WebReconnectToken.domainSettings:

params keyEffectDefault
reconnectDisabledno cookie issued; every received cookie is refused and erasedfalse
reconnectDayslifetime of one token, in days, sliding1 (24 h)
reconnectMaxDaysabsolute ceiling counted from the password entry (authAt), which never slides0 = none

Values may be booleans or numbers, or text typed in the form ("true", "1", "30"). Days are clamped between 0 and 3650. Missing domain, params Null or key absent: the defaults apply, never an error.

Example: with reconnectDays = 30 and reconnectMaxDays = 90, a user who comes back at least once a month stays logged in, but must type the password again at the latest 90 days after entering it.

Options passed explicitly to the constructor (validityDays, maxDays) win over the domain.

Lifecycle

MomentWhat happens
Login (BSPK_CONNECT_USER)the session is filled, vo_UserInfo.reconnectAuthAt is set to now; a token is issued and the setCookie action {vt_CookieName: "registerClient"; vt_CookieValue; vl_Days} is pushed before the reload action
Request from a guest session carrying the cookie (WebRequest.reconnectFromCookie)the cookie is checked: format, account exists and is active, hash matches, same IP, ceiling not reached; the session is then filled as at login
End of that requestthe token is rotated (same IP, same authAt, full duration within the ceiling); the old one stays valid for 60 s for parallel requests; the new cookie is sent. On a domain where reconnection is disabled, the received cookie is erased
Session alive but cookie missinga cookie is re-issued (not for .js, .css, .map, .svg, .png, .jpg, .ico, .pdf URLs)
/bweb/logoutthe session is cleared, the token of this device only is revoked and the cookie erased
First password definitionrevoked on every device
4D client "Users" listrevoked on every device
Identification on the WebSocket tunnelthe cookie identifies the user on the tunnel (see WebSocket and presence)

The browser gets the same expiry as the server: the cookie and its token expire together.

For the host's own accounts: BSPK_RECONNECT_TOKEN

A host with its own customer accounts (for example a CONTACT table) must not invent its own cookie. The shared component method BSPK_RECONNECT_TOKEN($action; $params) wraps the class:

  • actions: check, issue, resolve, revokeOne, revokeAll;
  • configuration, the same on every call: dataClass, field (an Object field the host creates), activeAttribute ("active" for BSPK_USER, none otherwise), validityDays, maxDevices, maxDays;
  • per action: issue → entity or uuid, ip; resolve → cookie, ip, checkIp; revokeOne → cookie; revokeAll → entity or uuid;
  • returns {success; error; cookie; entity}.

The /bweb/domain settings apply only to BSPK_USER, through the component's own calls; a host keeps its own options. Call check once: it returns the reason for a problem (unknown dataclass, missing field, field not of type Object, unknown active attribute) instead of a reconnection that silently never works.

Pitfalls

  • No field, no reconnection, no error. The bspkReconnect field is added to BSPK_USER by the STRUCTURE template; on another dataclass the host must create it. When it is missing, issuing logs WebReconnectToken.issue … reconnexion auto inactive in bspk.log, and the login still succeeds.
  • The account must be savable. If an ORDA event of the host class (validateSave) refuses to save the account, the token is not stored and the user will not be reconnected next time. Make sure your events let this save through.
  • The same IP is required for HTTP reconnection. A user whose public IP changes (mobile, VPN) is simply not reconnected. Behind nginx, set the X-Real-IP header, otherwise every user appears with IP 127.0.0.1.
  • Tokens issued by an earlier version have no password-entry time. The ceiling then counts from the first reconnection after the update.
  • revokeAll writes an empty collection, never Null: assigning Null to an object field and saving does not persist. Do the same in your own code.
  • To test as another user, go through /bweb/logout: it revokes the token and erases the cookie.

See also