A comprehensive Next.js application with integrated user management, Stripe payments, and points system built with Better Auth and Prisma.
- Email/Password Registration with username auto-generation
- Google OAuth integration
- User Profiles with avatar, country, IP tracking
- Admin Management with role-based access
- User Status management (active/inactive/suspended)
- Stripe Integration for secure payments
- One-time Purchases with instant point rewards
- Subscription Management (monthly/yearly/weekly)
- Order Tracking with detailed history
- Webhook Processing for real-time updates
- Two Point Types:
- One-time points (purchasable, with expiration)
- Subscription points (monthly grants, no expiration)
- Automatic Distribution for yearly subscriptions
- Complete Transaction History
- Point Spending with smart deduction logic
- Expiration Management with automated cleanup
- PostgreSQL Database with Prisma ORM
- Better Auth for modern authentication
- TypeScript throughout
- RESTful API design
- Webhook Security with signature verification
- Cron Jobs for automated tasks
- Framework: Next.js 15 with App Router
- Authentication: Better Auth
- Database: PostgreSQL with Prisma
- Payments: Stripe
- Styling: Tailwind CSS
- Language: TypeScript
-
Clone the repository
git clone <repository-url> cd template-basic
-
Install dependencies
pnpm install
-
Set up environment variables
cp .env.example .env
Edit
.envwith your actual values:# Database DATABASE_URL="postgresql://username:password@localhost:5432/your_db_name" # Better Auth BETTER_AUTH_SECRET="your-random-secret-key-here" BETTER_AUTH_URL="http://localhost:3000" NEXT_PUBLIC_BETTER_AUTH_URL="http://localhost:3000" # Google OAuth GOOGLE_CLIENT_ID="your-google-client-id" GOOGLE_CLIENT_SECRET="your-google-client-secret" # Stripe STRIPE_SECRET_KEY="sk_test_your-stripe-secret-key" STRIPE_PUBLISHABLE_KEY="pk_test_your-stripe-publishable-key" STRIPE_WEBHOOK_SECRET="whsec_your-webhook-secret" NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY="pk_test_your-stripe-publishable-key"
-
Set up the database
# Generate Prisma client pnpm db:generate # Push database schema pnpm db:push # Seed initial data pnpm db:seed
-
Start the development server
pnpm dev
- Go to Google Cloud Console
- Create a new project or select existing
- Enable Google+ API
- Create OAuth 2.0 credentials
- Add authorized redirect URI:
http://localhost:3000/api/auth/callback/google
- Create a Stripe account
- Get your API keys from the Stripe dashboard
- Set up webhook endpoint:
http://localhost:3000/api/webhooks/stripe - Subscribe to these events:
payment_intent.succeededpayment_intent.payment_failedcustomer.subscription.createdcustomer.subscription.updatedcustomer.subscription.deletedinvoice.payment_succeededinvoice.payment_failed
The application uses the following main entities:
User- User accounts with authentication dataSession- Better Auth session managementAccount- OAuth account linkingPrice- Product pricing with Stripe integrationOrder- Purchase orders and transactionsSubscription- Recurring subscription managementPointTransaction- Complete points transaction historyAppSetting- System configuration
POST /api/auth/sign-in- Email/password sign inPOST /api/auth/sign-up- User registrationGET /api/auth/callback/google- Google OAuth callback
GET /api/user/profile- Get user profilePUT /api/user/profile- Update user profile
GET /api/prices- List available pricesPOST /api/orders- Create new orderGET /api/orders- Get user orders
GET /api/subscriptions- Get user subscriptionsPOST /api/subscriptions/cancel- Cancel subscription
GET /api/points/balance- Get points balanceGET /api/points/transactions- Get transaction historyPOST /api/points/spend- Spend points
POST /api/webhooks/stripe- Stripe webhook handler
GET /api/settings- Get system settingsPOST /api/settings- Create/update settings
- User provides email/password or uses Google OAuth
- Username auto-generated from email prefix
- Stripe customer created automatically
- Registration IP and country tracked
- User selects product/plan
- Order created in pending state
- Stripe PaymentIntent generated
- Payment processed via Stripe
- Webhook confirms payment
- Points awarded automatically
- Order status updated to succeeded
- User subscribes to plan
- Stripe subscription created
- Initial points awarded (for yearly plans)
- Automatic point distribution:
- Monthly: Points awarded on each renewal
- Yearly: Points distributed monthly
-
Earning Points:
- Purchase rewards (one-time points with expiration)
- Subscription grants (monthly, no expiration)
-
Spending Points:
- Subscription points used first
- Then one-time points
- Complete transaction history maintained
-
Point Expiration:
- One-time points expire after configurable period
- Automatic cleanup via cron jobs
- Yearly subscription points distributed monthly
- Webhook Signature Verification for Stripe events
- SQL Injection Protection via Prisma ORM
- Password Hashing with bcrypt
- Session Management via Better Auth
- CSRF Protection built into Next.js
- Environment Variable protection
- Set up PostgreSQL database
- Configure all environment variables
- Set up Stripe webhooks with production URL
- Configure Google OAuth with production domains
pnpm db:migrate
pnpm db:seedpnpm build
pnpm startSet up a cron job to run point expiration cleanup:
# Daily at 2 AM
0 2 * * * curl -H "Authorization: Bearer YOUR_CRON_SECRET" https://yourdomain.com/api/cron/pointspnpm db:generate # Generate Prisma client
pnpm db:push # Push schema changes
pnpm db:migrate # Create and run migrations
pnpm db:seed # Seed initial data
pnpm db:studio # Open Prisma Studiosrc/
βββ app/ # Next.js App Router
β βββ api/ # API routes
β βββ auth/ # Auth pages
β βββ globals.css # Global styles
βββ components/ # React components
βββ lib/ # Utility libraries
β βββ services/ # Business logic services
β βββ utils/ # Helper functions
β βββ auth.ts # Better Auth config
β βββ db.ts # Database client
β βββ stripe.ts # Stripe client
βββ types/ # TypeScript types
-
Database Connection Issues
- Check DATABASE_URL format
- Ensure PostgreSQL is running
- Verify database exists
-
Stripe Webhook Issues
- Verify webhook secret
- Check endpoint URL
- Ensure proper event subscriptions
-
Google OAuth Issues
- Check redirect URI configuration
- Verify OAuth consent screen setup
- Ensure proper scopes configured
-
Points Not Distributing
- Check cron job setup
- Verify subscription status
- Review point transaction logs
- Check browser console for client-side errors
- Review server logs for API errors
- Use Prisma Studio to inspect database state
- Monitor Stripe dashboard for payment issues
This project is licensed under the MIT License.
For support, please create an issue in the repository or contact the development team.