feat(auth): @mosaic/auth — BetterAuth email/password setup (#68)

Co-authored-by: Jason Woltje <jason@diversecanvas.com>
Co-committed-by: Jason Woltje <jason@diversecanvas.com>
This commit was merged in pull request #68.
This commit is contained in:
2026-03-13 02:21:15 +00:00
committed by jason.woltje
parent 2b1723e898
commit 6e3cccc812
6 changed files with 544 additions and 4 deletions

42
packages/auth/src/auth.ts Normal file
View File

@@ -0,0 +1,42 @@
import { betterAuth } from 'better-auth';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
import type { Db } from '@mosaic/db';
export interface AuthConfig {
db: Db;
baseURL?: string;
secret?: string;
}
export function createAuth(config: AuthConfig) {
const { db, baseURL, secret } = config;
return betterAuth({
database: drizzleAdapter(db, {
provider: 'pg',
usePlural: true,
}),
baseURL: baseURL ?? process.env['BETTER_AUTH_URL'] ?? 'http://localhost:4000',
secret: secret ?? process.env['BETTER_AUTH_SECRET'],
basePath: '/api/auth',
emailAndPassword: {
enabled: true,
},
user: {
additionalFields: {
role: {
type: 'string',
required: false,
defaultValue: 'member',
input: false,
},
},
},
session: {
expiresIn: 60 * 60 * 24 * 7, // 7 days
updateAge: 60 * 60 * 24, // refresh daily
},
});
}
export type Auth = ReturnType<typeof createAuth>;