refactor: migrate backend to pnpm monorepo with Hono module architecture
- Replace Bun with pnpm 9 + Turborepo + tsx; apps/api renamed to apps/backend - Split backend into http/, modules/, lib/ layers mirroring tai-specguard - Add use-case + Result pattern, Problem Details RFC 7807, basePath /api/v1 - Mount Better Auth at /api/v1/auth, keep session-auth whitelist - Split Prisma schema into prisma/models/*, generate into generated/ - Rework packages/shared into lib/ + schemas/ with pagination DTOs - Implement health, groups, students, payments, waitlist modules - Add vitest suite with prisma mocks (21 tests), biome lint - Point web client to /api/v1/auth and /api/v1/groups/from-organization
This commit is contained in:
64
apps/backend/src/modules/auth/auth.ts
Normal file
64
apps/backend/src/modules/auth/auth.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { passkey } from '@better-auth/passkey';
|
||||
import { betterAuth } from 'better-auth';
|
||||
import { prismaAdapter } from 'better-auth/adapters/prisma';
|
||||
import { organization } from 'better-auth/plugins/organization';
|
||||
import { emailProvider } from '@/lib/email';
|
||||
import { getPrismaClient } from '@/lib/prisma';
|
||||
|
||||
export const auth = betterAuth({
|
||||
appName: 'Gruperly',
|
||||
basePath: '/api/v1/auth',
|
||||
database: prismaAdapter(getPrismaClient(), {
|
||||
provider: 'postgresql',
|
||||
}),
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
minPasswordLength: 8,
|
||||
sendResetPassword: async ({ user, url }) => {
|
||||
await emailProvider.sendPasswordResetEmail({
|
||||
email: user.email,
|
||||
url,
|
||||
name: user.name,
|
||||
})
|
||||
},
|
||||
},
|
||||
emailVerification: {
|
||||
sendOnSignUp: true,
|
||||
sendOnSignIn: true,
|
||||
autoSignInAfterVerification: true,
|
||||
sendVerificationEmail: async ({ user, token }) => {
|
||||
const webUrl = process.env.WEB_URL ?? 'http://localhost:6173'
|
||||
const verificationUrl = new URL('/verify-email', webUrl)
|
||||
verificationUrl.searchParams.set('token', token)
|
||||
await emailProvider.sendVerificationEmail({
|
||||
email: user.email,
|
||||
url: verificationUrl.toString(),
|
||||
name: user.name,
|
||||
})
|
||||
},
|
||||
},
|
||||
socialProviders: {
|
||||
...(process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET
|
||||
? {
|
||||
google: {
|
||||
clientId: process.env.GOOGLE_CLIENT_ID,
|
||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
plugins: [
|
||||
organization({
|
||||
acronym: 'GRP',
|
||||
allowUserToCreateOrganization: true,
|
||||
organizationLimit: 10,
|
||||
}),
|
||||
passkey({
|
||||
rpName: 'Gruperly',
|
||||
}),
|
||||
],
|
||||
trustedOrigins: [
|
||||
process.env.BETTER_AUTH_URL ?? 'http://localhost:4000',
|
||||
process.env.WEB_URL ?? 'http://localhost:6173',
|
||||
],
|
||||
})
|
||||
1
apps/backend/src/modules/auth/index.ts
Normal file
1
apps/backend/src/modules/auth/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { default as authRoutes } from './routes';
|
||||
8
apps/backend/src/modules/auth/routes.ts
Normal file
8
apps/backend/src/modules/auth/routes.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Hono } from 'hono';
|
||||
import { auth } from './auth';
|
||||
|
||||
const routes = new Hono();
|
||||
|
||||
routes.all('*', (c) => auth.handler(c.req.raw));
|
||||
|
||||
export default routes;
|
||||
Reference in New Issue
Block a user