Container Modules
Container modules provide a convenient way to set up Better Auth authentication in your InversifyJS dependency injection container. Each framework has its own specialized container module that automatically configures the necessary services, middleware, and controllers.
Available Container Modules
BetterAuthExpressContainerModule
Framework: Express
Sets up Better Auth for Express applications.
const container: Container = new Container();
const options = {
  database: new BetterSqlite3('./path/to/database.db'),
  emailAndPassword: {
    enabled: true,
  },
} as const satisfies BetterAuthOptions;
const betterAuthInstance = betterAuth(options);
const betterAuthExpressContainerModule: BetterAuthExpressContainerModule<
  typeof options,
  () => BetterAuth<typeof options>
> = BetterAuthExpressContainerModule.fromOptions(
  '/api/auth',
  betterAuthInstance,
);
await container.load(betterAuthExpressContainerModule);
const adapter: InversifyExpressHttpAdapter = new InversifyExpressHttpAdapter(
  container,
  {
    logger: true,
    useCookies: true,
  },
);
const application: express.Application = await adapter.build();
application.listen();
BetterAuthExpress4ContainerModule
Framework: Express v4
Sets up Better Auth for Express v4 applications.
const container: Container = new Container();
const options = {
  database: new BetterSqlite3('./path/to/database.db'),
  emailAndPassword: {
    enabled: true,
  },
} as const satisfies BetterAuthOptions;
const betterAuthInstance = betterAuth(options);
const betterAuthExpress4ContainerModule: BetterAuthExpress4ContainerModule<
  typeof options,
  () => BetterAuth<typeof options>
> = BetterAuthExpress4ContainerModule.fromOptions(
  '/api/auth',
  betterAuthInstance,
);
await container.load(betterAuthExpress4ContainerModule);
const adapter: InversifyExpressHttpAdapter = new InversifyExpressHttpAdapter(
  container,
  {
    logger: true,
    useCookies: true,
  },
);
const application: express.Application = await adapter.build();
application.listen();
BetterAuthFastifyContainerModule
Framework: Fastify
Sets up Better Auth for Fastify applications.
const container: Container = new Container();
const options = {
  database: new BetterSqlite3('./path/to/database.db'),
  emailAndPassword: {
    enabled: true,
  },
} as const satisfies BetterAuthOptions;
const betterAuthInstance = betterAuth(options);
const betterAuthFastifyContainerModule: BetterAuthFastifyContainerModule<
  typeof options,
  () => BetterAuth<typeof options>
> = BetterAuthFastifyContainerModule.fromOptions(
  '/api/auth',
  betterAuthInstance,
);
await container.load(betterAuthFastifyContainerModule);
const adapter: InversifyFastifyHttpAdapter = new InversifyFastifyHttpAdapter(
  container,
  {
    logger: true,
    useCookies: true,
  },
);
const application = await adapter.build();
await application.listen();
BetterAuthHonoContainerModule
Framework: Hono
Sets up Better Auth for Hono applications.
const container: Container = new Container();
const options = {
  database: new BetterSqlite3('./path/to/database.db'),
  emailAndPassword: {
    enabled: true,
  },
} as const satisfies BetterAuthOptions;
const betterAuthInstance = betterAuth(options);
const betterAuthHonoContainerModule: BetterAuthHonoContainerModule<
  typeof options,
  () => BetterAuth<typeof options>
> = BetterAuthHonoContainerModule.fromOptions(
  '/api/auth',
  betterAuthInstance,
);
await container.load(betterAuthHonoContainerModule);
const adapter: InversifyHonoHttpAdapter = new InversifyHonoHttpAdapter(
  container,
  {
    logger: true,
  },
);
const application = await adapter.build();
serve(application);
Factory Method: fromOptions()
All container modules provide a static fromOptions() method for easy instantiation:
public static fromOptions<TOptions extends BetterAuthOptions>(
  basePath: string,
  betterAuth: BetterAuth<TOptions>,
): ContainerModule<TOptions, () => BetterAuth<TOptions>>
Parameters
- basePath- The base URL path where Better Auth endpoints will be mounted (e.g.,- /api/auth)
- betterAuth- A configured Better Auth instance
Return Value
Returns a container module instance configured for the specific framework.
What Gets Registered
When you load a Better Auth container module, it automatically registers the following services in your container:
1. Better Auth Service
The core Better Auth instance is registered as a singleton service.
Service Identifier: betterAuthServiceIdentifier
2. Better Auth Middleware
Framework-specific middleware for handling authentication is registered as a singleton.
Service Identifier: betterAuthMiddlewareServiceIdentifier
3. Better Auth Controller
A framework-specific controller that handles all Better Auth endpoints (sign-in, sign-up, sign-out, etc.) is registered.
Service Identifier: betterAuthControllerServiceIdentifier
Service Identifiers
You can import service identifiers from '@inversifyjs/http-better-auth'
@Controller('/api')
class UserController {
  @ApplyMiddleware(betterAuthMiddlewareServiceIdentifier)
  @Get('/profile')
  public async getProfile(
    @HonoUserSession() session: UserSession<BetterAuthOptions>,
  ): Promise<UserSession<BetterAuthOptions>> {
    return session;
  }
}
Type Parameters
All container modules are generic and accept the following type parameters:
TOptions
Extends BetterAuthOptions - captures your specific Better Auth configuration for type safety.
TFactory
A factory function type that produces a BetterAuth<TOptions> instance. Usually () => BetterAuth<TOptions> when using fromOptions().
Complete Example
Here's a complete example showing how to set up a Hono application with Better Auth:
const options = {
  database: new BetterSqlite3('./auth.db'),
  emailAndPassword: {
    enabled: true,
  },
} as const satisfies BetterAuthOptions;
const betterAuthInstance = betterAuth(options);
// 2. Create container and load Better Auth module
const container: Container = new Container();
const betterAuthModule = BetterAuthHonoContainerModule.fromOptions(
  '/api/auth',
  betterAuthInstance,
);
await container.load(betterAuthModule);
// 3. Create a controller that uses authentication
@Controller('/api')
class UserController {
  @ApplyMiddleware(betterAuthMiddlewareServiceIdentifier)
  @Get('/profile')
  public async getProfile(
    @HonoUserSession() session: UserSession<typeof options>,
  ): Promise<UserSession<typeof options>> {
    return session;
  }
}
// 4. Register your controllers
container.bind(UserController).toSelf();
// 5. Build and run the application
const adapter = new InversifyHonoHttpAdapter(container);
const app = await adapter.build();
Related
- Parameter Decorators - For accessing session data in controllers
- BetterAuth- The Better Auth instance type
- UserSession- The session data structure