new TilesAuth(ctx, optionsopt)
BeNomad BeMap JavaScript API — TilesAuth (v2.0)
JWT lifecycle for the BeNomad Tiles Worker. Replaces the buggy
global `window.fetch` patch that the v1 prototype shipped with.
Responsibilities:
- POST credentials to `Context.getTilesLoginUrl()` and store the JWT.
- Parse the `exp` claim and schedule **proactive** renewal 5 min before
expiry; fall back to 55 min when `exp` is unparseable.
- Renew **reactively** on `401` errors caught from MapLibre's `error`
event, then trigger a `setStyle()` reload to retry pending tiles.
- On `document.visibilitychange` → `visible`: full re-login if the token
is already expired, proactive refresh if within 5 min, no-op otherwise.
- On `navigator.onLine === false`: queue the renewal until `online`.
- Expose `attachTransformRequest(nativeMap)` that injects the
`X-Session-Token` header on every request whose origin matches the
configured `tilesHost`. The closure captures `getToken()` not the
token value, so renewal is transparent.
- Keep the last 3 tokens in `_history` for diagnostics.
Tokens are stored according to `Context.tokenStorage`:
- `'session'` — `sessionStorage` (default; cleared on tab close)
- `'local'` — `localStorage` (persists across reloads)
- `'memory'` — never persisted
Parameters:
| Name | Type | Attributes | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ctx |
bemap.Context | ||||||||||||||
options |
Object |
<optional> |
Properties
|
- Since:
- 2.0.0
- Source:
Methods
(static) uninstallFetchInterceptor()
Uninstall the per-host registration. When the registry becomes empty,
restore the original `window.fetch` so SPA navigation does not leak
the patch across map lifecycles.
- Since:
- 2.0.0
- Source:
buildTransformRequest() → {function}
Build the MapLibre `transformRequest` function. Injects
`X-Session-Token` on every request whose origin matches `ctx.tilesHost`.
The closure captures `this.getToken` so token renewal is transparent.
Surface the result as `map.native = new maplibregl.Map({ transformRequest: auth.buildTransformRequest() })`.
Returns:
- Type
- function
destroy()
Detach lifecycle listeners. Call from `MapLibreMap.remove()`.
getHistory() → {Array.<{exp:Number}>}
Returns the last-3 history of superseded tokens — only the `exp` claim,
never the raw JWT.
Returns:
- Type
- Array.<{exp:Number}>
getToken() → {String|null}
Returns:
- Type
- String | null
handle401(nativeMap, currentStyleSpecopt) → {Promise.<String>}
Mark a tile / style request as failed with HTTP 401 — triggers a refresh
followed by a `setStyle` reload of the current style spec, so the renewed
token is picked up by every pending request.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
nativeMap |
Object | The map to reload — normally the bemap.MapLibreMap WRAPPER (so its overlay-replay listener fires and `{_internal:true}` is honoured), or a raw `map.native` instance (which ignores the option). | |
currentStyleSpec |
Object |
<optional> |
Style currently displayed, for the reload. |
Returns:
- Type
- Promise.<String>
isValid() → {Boolean}
Returns:
- Type
- Boolean
login(loginopt, passwordopt) → {Promise.<String>}
POST credentials and persist the new JWT.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
login |
String |
<optional> |
Defaults to `ctx.login`. |
password |
String |
<optional> |
Defaults to `ctx.password`. |
Returns:
The new JWT.
- Type
- Promise.<String>
logout(optionsopt) → {Promise.<void>}
Clear the token, the history, the storage, and the renewal timer.
Optionally POST to the Worker logout endpoint to revoke server-side.
Parameters:
| Name | Type | Attributes | Description | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
<optional> |
Properties
|
Returns:
- Type
- Promise.<void>
refresh() → {Promise.<String>}
Proactive renewal — re-uses the original credentials. Returns the new
token. Subscribers see exactly one `_onToken({ reason: 'refresh' })`
event per logical refresh; the synthetic `'login'` event is suppressed
during refresh to avoid the 3-events-per-cycle problem.
Returns:
- Type
- Promise.<String>
whenTokenReady() → {Promise.<(String|null)>}
Resolve with the current valid JWT, kicking off (or joining) a login
if no token is available. The synchronisation primitive that the
fetch interceptor uses to delay PMTiles range requests until login
completes — without making the MapLibreMap constructor itself async.
- Since:
- 2.0.0
- Source:
Returns:
- Type
- Promise.<(String|null)>