Skip to main content
zerotal

Documentation


Documentation / @zerotal/auth / AuthProvider

Class: AuthProvider

Defined in: auth/src/provider/AuthProvider.ts:48

Service provider that wires up the auth package. Registering it is all the setup most apps need.

On register it binds the hash, gate, and two_factor singletons and installs the app/policies auto-discovery, auth-schema, and RBAC-schema concerns; on boot it installs PersistUserMiddleware + RememberMeMiddleware (so ctx.user is populated on every request), teaches HttpContext.authorize(), and registers the make:policy and auth:sync-permissions CLI commands.

Remarks

The user loader resolves in the order app.withUserResolver(...)resolveUsing → the convention default (the registered AuthUser model). Guarding routes is a separate opt-in concern — see AuthMiddleware.

Example

// bootstrap/app.ts
import { AuthProvider } from "@zerotal/auth";

export default Application.create({ providers: [AuthProvider, /* … */] });

Extends

Constructors

Constructor

new AuthProvider(app): AuthProvider

Defined in: core/src/provider/ServiceProvider.ts:44

Parameters

app

Application

Returns

AuthProvider

Inherited from

ServiceProvider.constructor

Properties

provides

static provides: readonly ["hash", "gate", "two_factor"]

Defined in: auth/src/provider/AuthProvider.ts:49

Container tokens this provider registers. Required when using the array form of app.defer([Provider, ...]).

Example

static override provides = ['cache'] as const;

Overrides

ServiceProvider.provides


environments

static environments: AppEnvironment[]

Defined in: auth/src/provider/AuthProvider.ts:50

Environments in which this provider is active. Defaults to all of them.

Overrides

ServiceProvider.environments


dependsOn?

static optional dependsOn?: (app) => ServiceProvider[]

Defined in: core/src/provider/ServiceProvider.ts:35

Other providers this one needs. They are pulled into the application automatically (transitively, de-duplicated) and guaranteed to boot before this provider — so you only have to register the feature you want, not its plumbing. Reference the provider classes directly:

Parameters

app

Application

Returns

ServiceProvider

Example

static override dependsOn = [FlowProvider];

Inherited from

ServiceProvider.dependsOn


priority?

static optional priority?: number

Defined in: core/src/provider/ServiceProvider.ts:42

Boot-order tiebreak among providers with no dependsOn relationship. Lower boots earlier; defaults to 0. Handy for framework-core providers (e.g. static override priority = -100).

Inherited from

ServiceProvider.priority


app

protected app: Application

Defined in: core/src/provider/ServiceProvider.ts:44

Inherited from

ServiceProvider.app

Methods

resolveUsing()

static resolveUsing(fn): void

Defined in: auth/src/provider/AuthProvider.ts:69

Register the user loader used by PersistUserMiddleware to populate ctx.user.

Call this in bootstrap/app.ts before Application.create():

Parameters

fn

(id) => Promise<AuthUser | null>

Returns

void

Example

import { AuthProvider } from '@zerotal/auth';
import { User } from '../app/models/User.ts';

AuthProvider.resolveUsing((id) => User.find(id));

export default Application.create({ providers })...

onRegister()

onRegister(): void

Defined in: auth/src/provider/AuthProvider.ts:73

Register container bindings. Runs before any provider boots; do not resolve services here.

Returns

void

Overrides

ServiceProvider.onRegister


onBooting()

onBooting(): Promise<void>

Defined in: auth/src/provider/AuthProvider.ts:98

Resolve and prepare services before the app is considered booted.

Returns

Promise<void>

Overrides

ServiceProvider.onBooting


onBooted()

onBooted(): Promise<void>

Defined in: auth/src/provider/AuthProvider.ts:129

Run once all providers have booted and every binding is available.

Returns

Promise<void>

Overrides

ServiceProvider.onBooted


onStopping()

onStopping(): Promise<void>

Defined in: auth/src/provider/AuthProvider.ts:160

Run when the app begins a graceful shutdown; release resources here.

Returns

Promise<void>

Overrides

ServiceProvider.onStopping


onStarting()

onStarting(): Promise<void>

Defined in: core/src/provider/ServiceProvider.ts:53

Run just before the app starts accepting work (e.g. the HTTP server).

Returns

Promise<void>

Inherited from

ServiceProvider.onStarting


onStarted()

onStarted(): Promise<void>

Defined in: core/src/provider/ServiceProvider.ts:55

Run once the app has started.

Returns

Promise<void>

Inherited from

ServiceProvider.onStarted


onStopped()

onStopped(): Promise<void>

Defined in: core/src/provider/ServiceProvider.ts:59

Run once shutdown is complete.

Returns

Promise<void>

Inherited from

ServiceProvider.onStopped


onRequestReceived()

onRequestReceived(_ctx): Promise<void>

Defined in: core/src/provider/ServiceProvider.ts:66

Called before the middleware pipeline runs.

Parameters

_ctx

HttpContext

Returns

Promise<void>

Inherited from

ServiceProvider.onRequestReceived


onRequestProcessed()

onRequestProcessed(_ctx): Promise<void>

Defined in: core/src/provider/ServiceProvider.ts:69

Called after the pipeline completes and ctx.response is set.

Parameters

_ctx

HttpContext

Returns

Promise<void>

Inherited from

ServiceProvider.onRequestProcessed


onResponseSent()

onResponseSent(_ctx): Promise<void>

Defined in: core/src/provider/ServiceProvider.ts:72

Called after the response has been sent to the client.

Parameters

_ctx

HttpContext

Returns

Promise<void>

Inherited from

ServiceProvider.onResponseSent


replContext()

replContext(): Record<string, unknown>

Defined in: core/src/provider/ServiceProvider.ts:81

Variables to expose in bun zt repl. Override in any provider to make its facades available by name in the REPL session.

Returns

Record<string, unknown>

Example

override replContext() { return { DB, Mail }; }

Inherited from

ServiceProvider.replContext