Skip to main content

AuthModule

The AuthModule is the server-side module that orchestrates authentication. It is responsible for token generation, validation, and exposing the authentication API.

import { AuthModule } from '@archibald/auth';

new AuthModule(OPTIONS);

Options

  • Type: AuthModuleOptions

The AuthOptions object has the following properties:

NameTypeDescription
providersAuthProvider[]✔️ List of authentication providers.
options() => AuthOptions✔️ Dynamic accessor returning the options below. Must be a function — a static object throws when multi-country support is enabled.

strategy

PropertyTypeDescription
type'cookie' | 'header'Defines how tokens are sent. Default 'cookie'.
loadUserThroughAPIbooleanWhether to load user data through a backend API on every request.

token

PropertyTypeDescription
secretstring✔️ The primary JWE encryption secret. Must be exactly 32 characters — the module refuses to start otherwise.
secret_rotationstringOptional rotation secret, tried on decryption failure (zero-downtime secret rotation).
expirationTimestring | numberTTL for the session token. Default '5m'.
compressbooleanWhether to compress the token payload. Default false.
filter(user) => DefaultTokenUserSelects which user fields are embedded in the token. Default: identity.
cookieobjectCookie strategy only: cookie flags plus expiresIn (default '10m', sameSite: 'lax').

refresh

PropertyTypeDescription
secretstring✔️ Secret for the refresh token (also 32 characters).
secret_rotationstringOptional rotation secret.
expirationTimestring | numberTTL for the refresh token. Default '1y'.
cookieobjectCookie strategy only: cookie flags plus expiresIn (default '1y', sameSite: 'lax').
note

A legacy flat form (LegacyAuthModuleOptions, options spread at the top level next to providers) is still accepted. userCacheTime is not an AuthModule option — it belongs to the client-side SessionClient.

Endpoints Provided

Route paths are relative to your API schema (with the template's app.api config: /api/v2/...). Login-related routes are registered conditionally based on app.authentication.whitelistedProtocols — see OIDC Login Flow.

MethodPathRegistered whenPurpose
POSTauth/login'password' whitelistedPassword login (deprecated since v9).
GETauth/login'oidc' whitelistedRedirects to the IdP authorize URL.
POSTauth/token'oidc' whitelistedAuthorization code → token exchange (native flow).
GETauth/callback'oidc' whitelistedHandles the IdP redirect, sets session cookies, redirects to the return path.
POSTauth/refreshalwaysRefreshes the session token.
POSTauth/logoutalwaysLogs out and revokes tokens across all providers.
GETauth/checkalwaysLogged-in check (auth mode try).
GETauth/useralwaysReturns the current user's profile data (auth mode required).

The module also registers the jwt, cookie, and header Hapi auth strategies (which is why it must be registered before other modules whose routes reference them), and — in the cookie strategy — transparently refreshes expired sessions inside the auth scheme, re-setting the nct/ncr cookies on the response.

Usage Example

// src/server/module/server.tsx
import { AuthModule } from '@archibald/auth';
import { CommerceUserAuthProvider } from '@archibald/commerce/auth';

export class Server extends CoreServer {
public async initModules() {
await this.registerModules([
new AuthModule({
providers: [
new CommerceUserAuthProvider({
config: () => this.configService.get('hybris.api'),
credentials: () => this.configService.get('hybris.oauth')
})
],
options: () => ({
strategy: { type: 'cookie' },
token: { secret: this.configService.get('server.credentials.token.secret') },
refresh: { secret: this.configService.get('server.credentials.token.secret') }
})
})
]);
}
}

Relates to

  • AuthService: AuthModule uses the AuthService to handle the core logic of token encryption and provider coordination.
  • SessionClient: On the frontend, SessionClient makes requests to the endpoints exposed by AuthModule.
  • AuthProvider: AuthModule delegates credential validation to one or more providers.
  • ServerContext: Populates the server-side context with session data, accessible via getSession().