An example of how to integrate Flowglad into a Next.js project with BetterAuth. This project demonstrates the "Generation-Based Subscription Template Pricing Model".
- Next.js 16 - React framework with App Router
- BetterAuth - Modern authentication and user management
- Flowglad - Billing and subscription management
- Drizzle ORM - PostgreSQL database with type-safe queries
- TypeScript - Type safety throughout
- Tailwind CSS - Utility-first CSS framework
- shadcn/ui - Beautiful UI component library
- ✅ Authentication - Email/password authentication with BetterAuth
- ✅ Billing - Subscription management with Flowglad
- ✅ Database - PostgreSQL with Drizzle ORM migrations
- ✅ UI Components - Pre-built shadcn/ui components
- ✅ TypeScript - Full type safety across the stack
- Node.js >= 18.18.0
- pnpm >= 9.12.0
- PostgreSQL database
pnpm installCopy the example environment file:
cp .env.example .env.localFill in the required values in .env.local:
-
DATABASE_URL- PostgreSQL connection string- Example:
postgresql://user:password@localhost:5432/dbname
- Example:
-
BETTER_AUTH_SECRET- Secret key for BetterAuth session encryption- Generate with:
openssl rand -base64 32
- Generate with:
-
FLOWGLAD_SECRET_KEY- Secret key for Flowglad billing- Get your secret key from: https://flowglad.com
Generate and run database migrations:
pnpm db:generate
pnpm db:migratepnpm devOpen http://localhost:3000 to see the application.
pnpm dev- Start development serverpnpm build- Build for productionpnpm start- Start production serverpnpm lint- Run ESLintpnpm lint:fix- Fix ESLint errors automaticallypnpm type-check- Run TypeScript type checkingpnpm test- Run tests with Vitestpnpm db:generate- Generate database migrationspnpm db:migrate- Run database migrationspnpm db:studio- Open Drizzle Studio (database GUI)
├── src/
│ ├── app/ # Next.js App Router pages and routes
│ │ ├── api/ # API routes (BetterAuth, Flowglad)
│ │ ├── sign-in/ # Sign in page
│ │ └── sign-up/ # Sign up page
│ ├── components/ # React components
│ │ └── ui/ # shadcn/ui components
│ ├── lib/ # Utility functions and configurations
│ │ ├── auth.ts # BetterAuth configuration
│ │ ├── auth-client.ts # BetterAuth client
│ │ └── flowglad.ts # Flowglad configuration
│ └── server/ # Server-side code
│ └── db/ # Database schema and client
├── drizzle/ # Generated database migrations
├── pricing.yaml # Flowglad pricing model configuration
└── public/ # Static assets
This project uses BetterAuth for authentication. Users can sign up and sign in with email/password. The authentication state is managed server-side with secure cookies.
Flowglad is integrated for subscription and billing management. The Flowglad provider is configured to work with BetterAuth sessions. The pricing model is defined in pricing.yaml at the root of the project, which includes subscription plans, usage meters, and features.
The project uses Drizzle ORM with PostgreSQL. The schema includes the necessary tables for BetterAuth (users, sessions, accounts, verifications). You can extend the schema in src/server/db/schema.ts.
MIT