Skip to content

Boilerplate that includes authentication, authenticated routes, mailing, nextjs, postgresql db, Polar.sh (payments), husky, vitest

Notifications You must be signed in to change notification settings

berkerdemirer/nextjs-saas-boilerplate

Repository files navigation

SaaS Boilerplate

This is a Next.js project boilerplate designed to kickstart your SaaS application development. It includes essential features like authentication, database integration with Drizzle ORM, UI components with Shadcn/ui, and more.

Prerequisites

Before you begin, ensure you have the following installed:

Getting Started

Follow these steps to get your development environment up and running:

  1. Clone the repository:

    git clone <your-repository-url>
    cd saas-boilerplate
  2. Install dependencies: Choose your preferred package manager:

    npm install
    # or
    yarn install
    # or
    pnpm install
    # or
    bun install
  3. Set up environment variables:

    You'll need to provide values for:

    • DATABASE_URL: Your PostgreSQL connection string.
    • BETTER_AUTH_SECRET: A secret key for authentication.
    • POLAR_ACCESS_TOKEN: Polar acess token key
    • POLAR_WEBHOOK_SECRET: Webhook secret
    • RESEND_API_KEY: Your API key from Resend for sending emails.
    • GOOGLE_CLIENT_ID: Your Google OAuth client ID for social login.
    • GOOGLE_CLIENT_SECRET: Your Google OAuth client secret for social login.
  4. Run database migrations: Apply the database schema:

    npm run db:migrate

    (Optional) You can seed the database with initial data using:

    npm run db:seed
  5. Run the development server:

    npm run dev

    Open http://localhost:3000 (or your configured NEXT_PUBLIC_APP_URL) in your browser to see the result.

Application Configuration

The src/app-config.ts file centralizes various configuration settings for the application. You should modify this file to match your specific needs.

Authentication (Better Auth)

This boilerplate uses Better Auth for handling authentication and user management. The core configuration can be found in src/lib/auth.ts.

Key features implemented:

  • Database Integration: Uses the drizzleAdapter with your PostgreSQL database (schema defined in src/db/schema.ts).
  • Email & Password: Standard email and password sign-up and sign-in are enabled.
  • Social Login: Google authentication is implemented for easy sign-in. Define your client and secred ids in the .env
  • Email Verification:
    • Required for users signing up via email/password.
    • Verification emails are sent automatically on sign-up using the src/components/email-templates/verify-email.tsx template.
    • Users are auto-signed in and redirected post-verification.
  • Password Reset: Users can reset their passwords via email using the src/components/email-templates/reset-password.tsx template.
  • Welcome Email: A welcome email (src/components/email-templates/welcome.tsx) is sent when a new user record is created in the database.
  • Polar Integration:
    • Automatically creates Polar customers on user sign-up
    • You should create a product in Polar and insert their details to the db to dynamically fetch them for the better-auth/polar integration (see utils/auth)
    • Current Polar environment is set to sandbox
  • Next.js Integration: Uses the nextCookies() plugin for proper cookie handling within the Next.js framework.

You can customize email templates, authentication flows, and plugin configurations by modifying src/lib/auth.ts and the referenced email template components.

NB! Resend won't deliver your email if your domain is not authorized, that's why utils/app-config email.from setting is important. Set something that is valid as your domain.

Learn More

To learn more about the technologies used in this boilerplate, refer to their respective documentation:

Analytics

This boilerplate includes Umami for privacy-friendly analytics. The tracking script is configured in src/app/layout.tsx. You can:

  • Replace it with your own Umami instance by updating the script source and website ID
  • Switch to an alternative analytics solution like Plausible
  • Remove the analytics script entirely if not needed

About

Boilerplate that includes authentication, authenticated routes, mailing, nextjs, postgresql db, Polar.sh (payments), husky, vitest

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published