Reference / @getenginex/sdk-next / nextCookieTokenStore
nextCookieTokenStore()
nextCookieTokenStore(
options?):TokenStore
Cookie TokenStore, for the browser: the same session is readable on the server with readCookieTokenStore() from @getenginex/sdk-next/server (authenticated SSR). Optional: the SDK default remains localStorageTokenStore.
- Cookie:
<name>=<tokens>; Path=/; SameSite=Lax; Secure, withoutDomain(only the operator's domain) and withoutHttpOnly: the session refresh and the WebSocket?token=need the token in JavaScript. - Security: as with
localStorage, an XSS reads the token. The cookie still goes on every request to the app's domain (including navigations coming from another site, withSameSite=Lax): the app's server must only use it for reads (GET), never to perform actions on behalf of the cookie (CSRF). - Logs: the refresh token now goes on every request to the Next server (and to the proxies and CDN in front of it). Take the cookie out of the access logs (redaction of the
Cookieheader or of the cookie name). - HTTPS only: with
Secure, every browser silently discards the cookie on an HTTP origin that is notlocalhost(LAN IP,http://dev.domain:port). Onhttp://localhost, Chrome (89+) and Firefox (75+) accept theSecurecookie; other browsers, such as Safari, may not (see theSecurenote on Set-Cookie, in MDN). The browser also discards cookies above ~4 KB (both tokens go in a single cookie). In both cases,setchecks whether the cookie stuck and, if not, warns withconsole.warn(without the token) and does not throw; the login is not stored and the client stays without a session. The check is skipped when the page is outside the cookie'spath(the browser does not show it there) and withmaxAge: 0(the cookie is born expired). - Tabs: with the Cookie Store API (
cookieStore), it notifies subscribers when another tab changes the cookie; without it, only the changes made by this store. - Outside the browser (SSR of a Client Component):
get()returnsnullandset/cleardo not store anything, so no session stays in memory between server requests. On the server, usereadCookieTokenStore().
The cookie value never appears in errors: a malformed cookie becomes "no session".
Parameters
options?
NextCookieTokenStoreOptions = {}
Cookie name, lifetime and Path.
Returns
A TokenStore for createEnginexClient({ tokenStore }).
Throws
CONFIGURATION_ERROR if name is not usable as a cookie name.
Example
ts
'use client'
import { createEnginexClient } from '@getenginex/sdk'
import { nextCookieTokenStore } from '@getenginex/sdk-next'
export const enginex = createEnginexClient({
tokenStore: nextCookieTokenStore({ maxAge: 60 * 60 * 24 * 30 }),
})