[data-reveal]{opacity:1!important;transform:none!important}
AntelopeJS DMS
DocumentationSessions and sign-in

Sessions and sign-in

The auth hooks the module that owns sign-in registers, how page props and the interface language follow the session, and how a module flow opens a session through /auth/establish.

The frontend server keeps the session in a sealed, HttpOnly cookie and holds the access and refresh tokens on the server. The renderer itself serves no session, sign-in, OAuth or e-mail render route. Those routes belong to the DMS, which declares them in its own dms.server.ts. Without a module that registers auth hooks, pages carry no user and session, and a refused session is not renewed.

The screens a user sees, the sign-in form and the Security page, are covered in the illustrated built-in authentication guide. This page covers the server hooks behind those screens. Registering a hook does not create a sign-in UI.

Auth hooks

The server runs some session logic outside any route. It renews a session the backend refused on a page visit, builds the user and session page props, and sends a signed-out visit to sign in. The module that owns sign-in provides that logic:

dms.server.ts ts
server.registerAuth({
  signInPath: "/auth",
  signInExempt: ["/auth/**", "/onboarding"],
  refreshSession: renewSession, // (event, session) => the new session, or undefined
  publicSession: (session) => ({
    user: session.user,
    session: { accountId: session.accountId, activeTenantId: session.activeTenantId },
  }),
});
  • refreshSession runs once when the backend answers 401 to a signed-in page visit. The server then retries the visit with the session the hook wrote through event.session.write. It also runs before a request uses a session written more than 5 minutes ago, or stamped ahead of the server's clock, so the backend confirms the session at least that often. A request answered by a 404 or a 405 uses no session and is not renewed. When the backend refuses the session, the hook clears it with event.session.clear and returns undefined. When the renewal gives no session, the request runs signed out.
  • The sign-out route is registered with { signOut: true }, as in server.registerRoute("DELETE", "/api/_auth/session", logout, { signOut: true }). When the hook returns no session without clearing it, the backend being unreachable for instance, or throws, the sign-out runs with the session as sealed, so it still reaches the backend. When the renewal succeeds, it does so with a live access token. When the hook cleared the session, the sign-out runs signed out like any other route. The option counts only on a route of the module that registers the auth hooks.
  • publicSession returns the page props of a signed-in session. The server adds session.updatedAt, the time of the last write, and event.session.view() returns the same props to a route.
  • signInExempt patterns are /x, /x/* (one segment below) or /x/** (the page and everything below). When the option is left out, the patterns are <signInPath>/** and /onboarding, so a custom signInPath is exempt without more configuration.
  • signInPath is where a signed-out visit to a private page goes, with ?redirect=. It is /auth by default.

The server calls each hook with the object passed to registerAuth as this, so a hook written as a method can call a helper declared beside it.

The highest-priority module that registers auth hooks provides all of them. The server reports and ignores the hooks of any other module.

Session lifetime and revocation

The frontend server keeps no session store. The backend holds the sessions, so a sign-out reaches every frontend process.

  • Lifetime. The server opens a session for 30 days after its last write, whether or not it is persistent. A renewal writes it again and starts the 30 days over. The browser keeps a persistent session cookie for the same 30 days and drops the other kind when it closes. A session stamped more than 5 minutes ahead of the server's clock does not open, so the frontend processes' clocks must agree within that.
  • Revocation. A sign-out revokes the session on the backend. The backend refuses a revoked session's token at once, on every call. The frontend's own answers, such as the session view, the page props and the sign-in redirect, follow within 5 minutes: once a session is 5 minutes old, or when it is stamped ahead of the server's clock, the server has refreshSession renew it before a request uses it. The backend refuses to renew a revoked session, the hook clears it, and the request runs signed out. If refreshSession throws, that request also runs signed out, unless it is the sign-out, and the cookie stays.
  • A renewal in flight at the sign-out. A request that started a renewal before the sign-out, and answers after it, sets the session cookie again. Its tokens belong to the revoked session, so the backend refuses them at once. The frontend clears that session cookie within 5 minutes, the same bound as a copied one. The frontend clears only the session cookie. A cookie the sign-in module wrote during that renewal, such as a stored account, stays until the module removes it, and its tokens belong to the revoked session too.
  • Backend outage. While the backend cannot renew sessions, a session older than 5 minutes runs signed out, and the cookie stays until a renewal succeeds. Only the sign-out route runs with it.
Note

The DMS refreshes a token once per frontend process, for every request that needs it at the same time. A deployment scaled out to several processes uses sticky routing, so that a browser always reaches the same process. See Production.

Interface language

The interface language follows the session (useDmsUserSession().user.language), not the props of the page on screen. The first render takes the language from the session the page arrived with. The language changes when the session's language changes, after useDmsUserSession().fetch() or after a visit that brings a newer session.

  • A page that Inertia restores from history, or serves from its prefetch cache, carries the session it was fetched with. If that page carries an older session.updatedAt for the same account, it updates neither the session nor the language.
  • A session without a language, such as a signed-out one, keeps the language on screen. $i18n.setLocale() still switches the language at any time.
  • The dms_locale cookie remembers the language chosen in the application, for one year, with SameSite=Lax. A page without a signed-in user's language, such as a public sign-in page, first renders in that language. A visitor's choice survives a reload, and a signed-in user's own language comes first.
  • The CLI merges the i18n/locales/*.json files of the modules into one catalog per language, each in its own file outside the client entry. A language's catalog already holds the English message of every key it does not translate. The exception is the messages the renderer translates itself (error.network.*). A page therefore loads only its language's catalog, which the server preloads next to the entry.
  • te() reads only the current language's catalog, so it also answers true for a key that only English translates.
  • A page of a visitor who is not signed in, such as the sign-in page, reads a few dozen messages out of the thousands of its catalog. The server render records the messages it read and sends the page those in a dms-ssr-messages script, instead of preloading the catalog. The browser starts from them. When the visitor goes on, the browser loads the whole catalog. A visit waits for it before it shows its page, and a message the page lacks loads it too. Until then, te(), tm() and $i18n.getLocaleMessage() on such a page see only what the render read. A signed-in user's page loads the whole catalog, preloaded, as before.

Opening a session from a module flow

/auth/login, /auth/register and /auth/verify-2fa are not the only ways a visitor becomes authenticated. A module can own a flow that ends with an authenticated user and needs the session opened at its end. A self-service registration that completes after payment is one case, and an invitation being redeemed is another. POST /auth/establish is the generic form of those routes.

The browser names a backend endpoint and the payload to send it. $fetch is an auto-import of the renderer, available in a module's source files without an import (see Data loading). The declare line only gives this example the type that the renderer exports:

ts
declare const $fetch: typeof import("@antelopejs/dms-frontend/templates/vue/frontend-module").$fetch;

await $fetch("/auth/establish", {
  method: "POST",
  body: {
    endpoint: "/api/saas/register/finalize",
    payload: { /* whatever that backend route expects */ },
  },
});

The frontend server calls that endpoint itself, server to server, the same way it calls the backend's login route. It writes the session from the token pair the backend answers with. The browser never sends or receives a token. It gets back the same { user, account } body that the login route returns, and the two-factor and tenant-assignment outcomes work the same way as for a login.

Because the route turns a backend endpoint into a login, it calls only the endpoints declared for it. A backend module declares its own when it registers its frontend:

src/index.ts ts
import path from "node:path";
import { AddFrontendModule } from "@antelopejs/interface-dms/page";

await AddFrontendModule({
  name: "@antelopejs/dms-saas-frontend-vue",
  sourcePath: path.join(__dirname, "../frontend-vue"),
  renderer: { name: "vue", version: "3" },
  authEstablishEndpoints: ["/api/saas/register/finalize"],
});

The DMS gathers the declared endpoints into the manifest, and dev and build write them into the workspace. DMS_AUTH_ESTABLISH_ENDPOINTS adds endpoints that no module declares, comma-separated. The allow-list is the union of both.

Each entry is matched exactly against absolute /api/ paths, with no prefix match, no query string and no traversal. An undeclared endpoint gets a 403 and is never called. The route accepts POST only and requires the frontend's own origin, like every other auth action.