A modern authentication solution built with Next.js 15, Prisma, and better-auth. This project provides a secure and customizable authentication system for web applications with form validation using Zod and TanStack React Query.
- π Secure authentication using better-auth
- π¦ PostgreSQL database integration with Prisma
- π§© Form validation with Zod
- π API state management with TanStack React Query
- π Built with Next.js 15 and TurboCache for optimal performance
- π¨ UI components with Tailwind CSS and Shadcn/UI
- π Dark mode support with next-themes
- π Toast notifications with Sonner
- Node.js 18.x or higher
- PostgreSQL database
- npm or yarn
git clone https://github.com/Rogenecarl/better-auth-nextjs-authentication-boilerplater-with-userRoles.git
cd betterauthnpm install
# or
yarn installCopy the example.env file to .env:
cp example.env .envFill in the required environment variables:
BETTER_AUTH_SECRET= Your better auth secret || generate from better auth
BETTER_AUTH_URL=http://localhost:3000
# from server to client
NEXT_PUBLIC_API_URL=http://localhost:3000
# Connect to PostgreSQL via connection pooling
DATABASE_URL=postgresql://username:password@host:port/database
# Direct connection to the database. Used for migrations
DIRECT_URL=postgresql://username:password@host:port/database
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
NODEMAILER_USER=
NODEMAILER_APP_PASSWORD=
npx prisma db push
npx prisma generateStart the development server:
npm run dev
# or
yarn devOpen http://localhost:3000 with your browser to see the application.
Then install the external dependencies:
# Authentication and database
npm install better-auth @prisma/client
# Form validation and data fetching
npm install zod @tanstack/react-query
# UI components
npm install sonner next-themes
# Install shadcn components (CLI)
npx shadcn-ui@latest init
# for password hashing
npm install @node-rs/argon2
# for email verification
npm install nodemailer
npm install @types/nodemailer -DThe application uses the following data models:
- User: Core user data (name, email, etc.)
- Session: User sessions with expiration
- Account: Provider accounts (OAuth, password)
- Verification: For email verification
- Post: Example model for user content
βββββββββββββββ βββββββββββββ
β User β β Session β
βββββββββββββββ€ βββββββββββββ€
β id β β id β
β createdAt β β createdAt β
β updatedAt β β updatedAt β
β name β β expiresAt β
β email βββββββββ€ userId β
β emailVerified β token β
β image β β ipAddress β
βββββββββββββββ β userAgent β
β² βββββββββββββ
β
β ββββββββββββββββββ
β β Verification β
β ββββββββββββββββββ€
β β id β
β β createdAt β
β β updatedAt β
β β identifier β
β β value β
β β expiresAt β
β ββββββββββββββββββ
β
ββββββββ΄βββββββββ βββββββββββββ
β Account β β Post β
βββββββββββββββββ€ βββββββββββββ€
β id β β id β
β createdAt β β createdAt β
β updatedAt β β updatedAt β
β accountId β β title β
β providerId β β content β
β accessToken βββββββββ€ userId β
β refreshToken β βββββββββββββ
β idToken β
β password β
β userId β
βββββββββββββββββ
The project uses better-auth for authentication:
- Authentication is configured in
src/lib/auth.ts - Client-side authentication hooks are available in
src/lib/auth-client.ts - API routes are set up in
src/app/api/auth/[...all]/route.ts
- Registration: Users can register with email/password or OAuth providers
- Login: Secure login with email/password or OAuth
- Session Management: Sessions are stored in the database with expiration
- Profile Management: Users can update their profile information
Forms are validated using Zod schemas with a custom useZodForm hook that:
- Defines validation rules for form fields
- Provides real-time validation feedback
- Integrates with TanStack React Query for API calls
- Handles error states and displays user-friendly messages
src/
βββ app/ # Next.js App Router
β βββ api/ # API Routes
β β βββ auth/ # Auth API Routes
β β βββ login/ # Login Page
β β βββ register/ # Registration Page
β βββ profile/ # User Profile Page
β βββ layout.tsx # Root Layout
βββ components/ # React Components
β βββ hooks/ # Custom Hooks
β βββ providers/ # Context Providers
β βββ ui/ # UI Components
β βββ login-form.tsx # Login Form Component
β βββ register-form.tsx # Registration Form Component
βββ generated/ # Generated Prisma Client
βββ lib/ # Utility Functions
β βββ auth.ts # Auth Configuration
β βββ auth-client.ts # Client-side Auth Hooks
β βββ prisma.ts # Prisma Client
β βββ utils.ts # Utility Functions
Build the application:
npm run build
# or
yarn buildStart the production server:
npm run start
# or
yarn startTo add OAuth providers like Google, GitHub, etc., modify src/lib/auth.ts:
export const auth = betterAuth({
// Existing config...
// Add OAuth providers
oauth: {
providers: [
{
id: "google",
name: "Google",
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
},
// Add more providers as needed
],
},
});Create new pages in the src/app directory following Next.js App Router conventions.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request