Introduction
Better Auth is a comprehensive authentication library for TypeScript that provides secure, type-safe authentication with excellent developer experience. The InversifyJS HTTP framework provides seamless integration with Better Auth, allowing you to easily add authentication to your applications across Express, Express 4, Fastify, and Hono.
Features
- Type-safe authentication: Full TypeScript support with type inference
- Multiple adapters: Support for Express, Express 4, Fastify, and Hono
- Session management: Built-in session handling with middleware integration
- Flexible configuration: Easy setup with various authentication providers
- Parameter decorators: Convenient access to user sessions in your controllers
Quick Start
The integration provides container modules that automatically set up Better Auth endpoints and middleware for each supported framework.
Start by installing the necessary packages:
- npm
- yarn
- pnpm
npm install @inversifyjs/http-better-auth better-auth
yarn add @inversifyjs/http-better-auth better-auth
pnpm add @inversifyjs/http-better-auth better-auth
Then, choose the appropriate container module for your framework:
- Express
- Express 4
- Fastify
- Hono
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();
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();
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();
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);
Refer to the Better Auth Documentation for detailed setup instructions and advanced configuration options.