REST Controllers
This page documents the bundled AuthController — its seven endpoints (five @Public(), two ARBAC-gated), their decorators, request/response shapes, and how to subclass it to extend the workflow allow-list.
AuthController
import { AuthController } from "@aooth/auth-moost";
app.registerControllers(AuthController);Decorated @Controller("auth") @ArbacResource("auth"). Constructor takes a DI-provided AuthCredential plus an @Optional() UserService:
constructor(
protected readonly auth: AuthCredential,
@Optional() protected readonly users?: UserService,
)users is optional — only GET /auth/invite/post-redemption reads it directly (returns 500 when unset); the other routes work without a UserService on the controller (the workflow-dispatching routes resolve UserService through the workflow, not this field).
Five endpoints are @Public(); two are ARBAC-gated. The public ones enforce authentication inside the handler bodies as defence-in-depth — every endpoint that requires a context calls useAuth().getAuthContext() and returns 401 if it's null. The reason they're public is that the routes themselves must be reachable to anonymous callers (a logged-out user trying to log in, an expired-access-token caller refreshing, an unauthenticated invitee redeeming a magic link). The remaining two — POST /auth/change-password and POST /auth/add-mfa — are not @Public(): they are authenticated self-service flows gated by @ArbacResource(...) @ArbacAction("self"), so the guard rejects anonymous (401) and unprivileged (403) callers before the handler runs (see their rows).
Endpoint table
| Method | Path | Decorators | Body | Response | Notes |
|---|---|---|---|---|---|
POST | /auth/logout | @Public() | AuthLogoutBody { refreshToken? } | AuthOkResponse { ok: true } | Defence-in-depth 401 if null context. Revokes the current session's whole token family (revokeSession), with token-level revokes as fallback, then clearCookies(). |
POST | /auth/refresh | @Public() + two @RateLimit policies (aooth.refresh 150/5m per IP, aooth.refresh-token 10/1m per token) | AuthRefreshBody { refreshToken? } | AuthLoginResponse | Falls back to the refresh cookie. 401 on AuthError. Writes the rotated pair as cookies; the body carries the tokens under exposeTokensInBody, and always when the refresh token was sent in the body (that client already holds tokens). |
GET | /auth/status | @Public() | — | AuthContext | 401 when no context. |
POST | /auth/trigger | @Public() @WfTrigger({ allow: DEFAULT_AUTH_WORKFLOWS }) | { wfs?, input?: { action?, formData? } } | WfFinished envelope | The single entry-point covering auth/login/flow, auth/invite/start, auth/recovery/flow, auth/signup/flow — for both starting and resuming. A wfs of any other workflow is refused with 403. |
GET | /auth/invite/post-redemption | @Public() @RateLimit("30/5m", { id: "aooth.invite-post-redemption" }) | ?uid=<userId> | WfFinished envelope | Idempotent "already accepted" envelope for re-clicked invite links after the wf state row is evicted. Needs UserService. Unknown id and pending invite both answer the same 404. |
POST | /auth/change-password | @ArbacResource("auth.change-password") @ArbacAction("self") @WfTrigger({ allow: [CHANGE_PASSWORD_WORKFLOW] }) | { wfs?, input?: { action?, formData? } } | WfFinished envelope | Gated trigger for auth/change-password/flow — NOT @Public(), NOT in DEFAULT_AUTH_WORKFLOWS. 401 if unauthenticated, 403 unless the role grants allow("auth.change-password", "*"). The method-level @ArbacResource overrides the class "auth". See Workflows — Change password. |
POST | /auth/add-mfa | @ArbacResource("auth.add-mfa") @ArbacAction("self") @WfTrigger({ allow: [ADD_MFA_WORKFLOW] }) | { wfs?, input?: { action?, formData? } } | WfFinished envelope | Gated trigger for auth/add-mfa/flow — the "Manage MFA" flow (add / change / remove, step-up first), the profile-maintenance twin of change-password. NOT @Public(), NOT in DEFAULT_AUTH_WORKFLOWS. 401/403 identical to above but keyed on allow("auth.add-mfa", "*"). See Workflows — Manage MFA. |
DEFAULT_AUTH_WORKFLOWS is the exported as const allow-list:
export const DEFAULT_AUTH_WORKFLOWS = [
"auth/login/flow",
"auth/invite/start",
"auth/recovery/flow",
"auth/signup/flow",
] as const;The two gated flows are not in this list — auth/change-password/flow (CHANGE_PASSWORD_WORKFLOW) and auth/add-mfa/flow (ADD_MFA_WORKFLOW) are each dispatched from their own ARBAC-guarded route above, so they are unreachable from the public /auth/trigger. auth/signup/flow is public (gated by resolveSignupPolicy().allowSignup, off by default) — see Workflows — Self-signup.
POST /auth/logout
// minimal
POST /auth/logout
Cookie: aooth_session=<access>
Content-Type: application/json
{ "refreshToken": "<optional>" }Response: { "ok": true }.
Behaviour:
- If
useAuth().getAuthContext()is null →throw HttpError(401, "Not authenticated")(code: "AOOTH_NOT_AUTHENTICATED"). (Defence-in-depth — the route is@Public()only so the handler is reachable.) - Ends THIS device's whole session family. Resolves the current
sessionIdfrom the auth context and best-effort callsauth.revokeSession(userId, sessionId)— revoking every token in the family (access + refresh + all rotations) on a store that can enumerate. This is what "log out" means now that a session is a token family: the SPA can't read the httpOnly refresh cookie, and that cookie's narrow path keeps it off/auth/logout, so the token-level revokes below can't reach the refresh credential on their own. - Token-level fallback. Pulls the access token via
useAuth().extractToken()and arefreshTokenfrom the body, and best-effort revokes each. This covers stateless stores (whererevokeSessionis a no-op) and is a harmless no-op once the family is already gone. Failures are silently swallowed (logout must not block on a denied revoke). - Calls
useAuth().clearCookies()to clear bothSet-Cookieheaders.
Net effect: a logged-out device disappears from auth.listSessions(userId) immediately on a stateful store — it no longer lingers for the refresh TTL — while the user's other devices keep working.
Refresh cookie path is narrow on purpose
The default refreshCookie.path is /auth/refresh (auto-derived to the controller's actual mount, see Config Reference), so browsers won't send it to any other path. /auth/logout therefore can't read the refresh cookie — which is exactly why logout revokes the whole family by sessionId rather than relying on the cookie. A body refreshToken field is still accepted as a belt-and-suspenders fallback (and for stateless stores). If you widen the path (e.g. to /), you lose the CSRF-resistance benefit — the refresh cookie is now sent on every request.
POST /auth/refresh
// minimal
POST /auth/refresh
Cookie: aooth_refresh=<refresh>
Content-Type: application/json
{}Response: AuthLoginResponse.
Behaviour:
- Pulls
refreshTokenfrom the body, falls back to the refresh cookie (path-scoped so it actually arrives here). - If missing → 401
Missing refresh token(MISSING_REFRESH_TOKEN). - Calls
auth.refresh(refreshToken). OnAuthError→ 401Invalid refresh token(INVALID_REFRESH_TOKEN). When the refused token came from the refresh cookie, the 401 also clears both auth cookies (Max-Age=0, since 0.2.1), so a dead refresh cookie doesn't linger in a persistent jar. A refused body token, or a request that carried no refresh token at all (such as a cross-site form POST), leaves the cookies alone. Either way, treat a 401 here as "signed out" and send the user to login. - Writes new
Set-Cookiefor both access + refresh viauseAuth().writeCookies(issue)(persistent cookies get a freshMax-Age— see Config — persistent cookies). - Returns
useAuth().buildLoginResponse(userId, issue)—userId+ expiry timestamps, plusaccessToken/refreshTokenonly whenexposeTokensInBodyis on (default!enableCookie).
Rate limited when rateLimitInterceptors are installed, by the two refresh policies in the endpoint table — per client IP (checked before the auth guard) and per presented refresh token. Over a limit the call answers the coded 429 (RATE_LIMITED, Retry-After). Values, keys and re-tuning: Bundled limits.
GET /auth/status
Response: AuthContext (the stashed context).
// minimal
GET /auth/status
Cookie: aooth_session=<access>Returns { userId, claims, expiresAt, ... }. 401 if no context (defence-in-depth — null bodies are not acceptable for a status check).
Use this for an SPA's "am I logged in?" probe on page load. Pair with /auth/refresh to bootstrap: if /auth/status returns 401, the SPA tries /auth/refresh and re-probes.
POST /auth/trigger
The single workflow trigger endpoint. The request body shape is @atscript/moost-wf's wf-trigger contract — the wf engine reads the action + form data directly from body.input:
POST /auth/trigger
Content-Type: application/json
{
"wfs": "<resume-token>", // resume an in-flight workflow (absent on start)
"input": {
"action": "forgotPassword", // alt-action name (forgotPassword, signup, magicLink, sso-<id>)
"formData": { /* values for the current paused step */ }
}
}On start (no wfs), the workflow id is supplied by the client's <AsWfForm :name="…"> (e.g. auth/login/flow) — the trigger rejects any id not in allow. On resume, the wfs token carries the id, and it is checked against the same allow list (see Workflows — trigger allow lists). Response: WfFinished envelope (paused form, finished, or aborted) — see Workflows.
Behaviour:
- The handler body is intentionally empty (
override triggerWf(): void { }).@WfTrigger({ allow: DEFAULT_AUTH_WORKFLOWS })wraps it as adefineAfterInterceptorthat:- Instantiates
WfTriggerProvider. - Reads the trigger payload from body/query/cookie according to the provider's
tokenwire config. - Dispatches into the requested workflow — a start or a resume of any workflow not in
allowis refused with 403 (a refusedwfsis not consumed). - Pauses on a form-input step → returns a paused envelope.
- Finishes → returns the
WfFinishedenvelope.
- Instantiates
- Cookies attached by the login/auto-login finalize steps flow through the
cookiesfield on the envelope (built viauseAuth().buildFinishedCookies(issue)).
GET /auth/invite/post-redemption
A side route for re-clicked invite magic links. When an invitee clicks an already-redeemed link, the wf state row has been evicted (the resume returns 410), so the workflow can't re-enter to render the idempotent "already accepted" envelope. The magic-link URL carries the invitee uid (from buildMagicLinkUrl(kind, token, { userId })), and this route rebuilds the same envelope from it:
GET /auth/invite/post-redemption?uid=<userId>Resolves the user via the @Optional() UserService (500 if no UserService is wired) and returns a WfFinished "already accepted" envelope when the invite is redeemed.
| Situation | Response |
|---|---|
| invite redeemed | WfFinished "already accepted" envelope |
unknown uid or invite still pending | 404 "Invite not found" — identical, so the route reveals neither which ids exist nor their state |
uid missing | 400 |
| route switched off (see below) | 404 "Invite not found" for every id |
- Rate limited —
@RateLimit("30/5m", { id: "aooth.invite-post-redemption" })per client IP. Like every@RateLimit, it is enforced only whenrateLimitInterceptorsare installed. - Switch it off when your app does not use magic-link invites: override
protected invitePostRedemptionEnabled(): booleanto returnfalse(a subclass cannot unregister an inherited route). - Override
protected resolveInvitePostRedemption()to keeploginUrl/alreadyAcceptedRedirectUrlin sync with your workflow opts; it may also returnmessages— pass the same value asAuthWorkflowOpts.messagesso the envelope's texts match (see Messages).
DTOs
// AuthLogoutBody
interface AuthLogoutBody {
refreshToken?: string;
}
// AuthRefreshBody
interface AuthRefreshBody {
refreshToken?: string;
}
// AuthLoginResponse
interface AuthLoginResponse {
userId: string;
accessExpiresAt: number;
refreshExpiresAt?: number;
accessToken?: string; // only when exposeTokensInBody (default !enableCookie)
refreshToken?: string; // same
}
// AuthOkResponse
interface AuthOkResponse {
ok: true;
}accessToken and refreshToken are present only when the guard's exposeTokensInBody is on. It defaults to !enableCookie, so a default cookie deployment omits them — the browser relies on the httpOnly cookies, and an XSS calling /auth/refresh cannot read a fresh refresh token. Since 0.2 — before, the fields followed enableBearer (default true). Token clients sharing a cookie deployment set exposeTokensInBody: true. The userId and accessExpiresAt fields are always present for SPAs to schedule their next status probe.
Subclassing AuthController
The most common subclass extends the workflow allow-list. Two patterns:
Pattern A — override triggerWf with a wider allow
import { AuthController, DEFAULT_AUTH_WORKFLOWS, Public, WfTrigger } from "@aooth/auth-moost";
import { AuthCredential } from "@aooth/auth";
import { UserService } from "@aooth/user";
import { Inherit, Controller, Optional } from "moost";
import { Post } from "@moostjs/event-http";
@Inherit()
@Controller("auth")
class MyAuthController extends AuthController {
constructor(auth: AuthCredential, @Optional() users?: UserService) {
super(auth, users); // forward both so post-redemption keeps working
}
@Post("trigger")
@Public()
@WfTrigger({
allow: [...DEFAULT_AUTH_WORKFLOWS, "project.handover", "tenant.onboard"],
})
override triggerWf(): void {
// body intentionally empty — see AuthController.triggerWf
}
}@Inherit() is mandatory on the subclass
Without @Inherit(), moost re-scans method decorators only on the subclass body and the unoverridden endpoints (logout, refresh, status) disappear from the route table. The @Inherit() decorator flows the parent class's @Get('status') / @Post('logout') / @Post('refresh') metadata down.
What a re-decorated trigger inherits
The override's @WfTrigger({...}) replaces the base route's trigger options as a whole: its allow is the only list checked (it can narrow as well as widen the base list), and its hooks (renderEnvelope, around, …) are the only route-level hooks. The trigger still runs once per request. Repeat any base option you want to keep. An override without its own @WfTrigger keeps the base route's options unchanged.
Pattern B — replace triggerWf entirely
If you need conditional logic — e.g. routing to a different wf based on a header — return a non-undefined value from your override; the after-interceptor will skip the default handle() dispatch and use your return value as the response.
@Post("trigger")
@Public()
@WfTrigger({ allow: [...DEFAULT_AUTH_WORKFLOWS] })
override async triggerWf(): Promise<unknown> {
const tenant = useRequest().rawRequest.headers["x-tenant"];
if (tenant === "demo") return { wfid: "demo.login.short-circuit" };
// returning undefined falls through to the default WfTriggerProvider.handle()
return undefined;
}See Workflows for the provider subclassing pattern (state store, outlets, token wire).
See also
- Workflows — what happens inside
/auth/trigger. - AuthGuard & useAuth — the composable used by every endpoint above.
- Config Reference —
AuthOptionsfield-by-field.