Skip to content

dekema9924/Next-Auth

Repository files navigation

Next.js Authentication System with Better Auth

A modern, full-featured authentication system built with Next.js 15, Better Auth, Prisma, and TypeScript. This project provides a complete authentication solution with email verification, password management, and user profile features.

Features

Authentication

  • βœ… Email & Password Authentication - Secure sign up and sign in
  • βœ… Email Verification - Verify user emails with OTP
  • βœ… Forgot Password - Password reset via email
  • βœ… Change Password - Allow users to update their password
  • βœ… Social Login - Google and GitHub OAuth integration
  • βœ… Session Management - Secure session handling with Better Auth

User Management

  • βœ… User Profiles - View and edit user information
  • βœ… Profile Pictures - Upload images to Cloudinary
  • βœ… Account Settings - Manage personal information
  • βœ… Dashboard - Protected user dashboard

UI/UX

  • βœ… Responsive Design - Mobile-first, works on all devices
  • βœ… Modern UI - Clean, professional interface
  • βœ… Toast Notifications - Real-time user feedback
  • βœ… Form Validation - React Hook Form with validation
  • βœ… Loading States - Clear feedback during async operations

πŸ› οΈ Tech Stack

πŸ“‹ Prerequisites

Before you begin, ensure you have the following installed:

  • Node.js 18.x or higher
  • npm, yarn, pnpm, or bun
  • PostgreSQL (or your preferred database)
  • Cloudinary account (for image uploads)

πŸš€ Getting Started

1. Clone the repository

git clone <your-repo-url>
cd next-authentication

2. Install dependencies

npm install
# or
yarn install
# or
pnpm install
# or
bun install

3. Set up environment variables

Create a .env.local file in the root directory:

# Database
DATABASE_URL="postgresql://user:password@localhost:5432/mydb"

# Better Auth
BETTER_AUTH_SECRET="your-secret-key-here"
BETTER_AUTH_URL="http://localhost:3000"

# OAuth Providers (Optional)
GOOGLE_CLIENT_ID="your-google-client-id"
GOOGLE_CLIENT_SECRET="your-google-client-secret"

GITHUB_CLIENT_ID="your-github-client-id"
GITHUB_CLIENT_SECRET="your-github-client-secret"

# Cloudinary (for image uploads)
NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME="your-cloud-name"
CLOUDINARY_API_KEY="your-api-key"
CLOUDINARY_API_SECRET="your-api-secret"

# Email (Optional - for email verification)
EMAIL_SERVER="smtp://user:password@smtp.example.com:587"
EMAIL_FROM="noreply@example.com"

4. Set up the database

# Generate Prisma Client
npx prisma generate

# Run migrations
npx prisma migrate dev

# (Optional) Seed the database
npx prisma db seed

5. Run the development server

npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev

Open http://localhost:3000 with your browser to see the result.

πŸ“ Project Structure

β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ auth/          # Authentication API routes
β”‚   β”‚   └── upload/        # File upload routes
β”‚   β”œβ”€β”€ auth/
β”‚   β”‚   β”œβ”€β”€ signin/        # Sign in page
β”‚   β”‚   β”œβ”€β”€ signup/        # Sign up page
β”‚   β”‚   └── verify-otp/    # Email verification
β”‚   β”œβ”€β”€ dashboard/         # Protected dashboard
β”‚   β”œβ”€β”€ forgot-password/   # Password reset
β”‚   └── reset-password/    # Reset password with token
β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ ui/               # Reusable UI components
β”‚   β”œβ”€β”€ Header.tsx        # Navigation header
β”‚   β”œβ”€β”€ UpdateProfile.tsx # Profile update modal
β”‚   └── PasswordInputs.tsx # Password form component
β”œβ”€β”€ context/
β”‚   └── ModalContext.tsx  # Global modal state
β”œβ”€β”€ lib/
β”‚   β”œβ”€β”€ auth.ts          # Better Auth configuration
β”‚   β”œβ”€β”€ prisma.ts        # Prisma client
β”‚   β”œβ”€β”€ session.ts       # Session utilities
β”‚   └── updateProfile.ts # Profile update logic
β”œβ”€β”€ prisma/
β”‚   └── schema.prisma    # Database schema
└── public/              # Static assets

πŸ”§ Configuration

Better Auth Setup

Better Auth is configured in lib/auth.ts. You can customize:

  • Email providers
  • OAuth providers (Google, GitHub, etc.)
  • Session duration
  • Security settings

Prisma Schema

The database schema is defined in prisma/schema.prisma. Main models include:

  • User - User account information
  • Session - Active user sessions
  • Account - OAuth account connections
  • VerificationToken - Email verification tokens

Cloudinary Integration

Images are uploaded to Cloudinary for:

  • Profile pictures
  • User-generated content

Configure your Cloudinary credentials in .env.local.

Authentication Flow

  1. Sign Up

    • User creates account with email and password
    • Verification email sent (if configured)
    • Account created in database
  2. Email Verification

    • User receives OTP code
    • Enters code on verification page
    • Email marked as verified
  3. Sign In

    • User enters credentials
    • Session created on success
    • Redirected to dashboard
  4. Password Reset

    • User requests reset via email
    • Receives reset link with token
    • Creates new password
    • Token invalidated after use

Customization

Styling

This project uses Tailwind CSS. Customize the theme in tailwind.config.ts:

module.exports = {
  theme: {
    extend: {
      colors: {
        // Add your custom colors
      },
    },
  },
};

Components

All UI components are in the components/ directory and can be customized to match your brand.

Key Scripts

# Development
npm run dev          # Start dev server
npm run build        # Build for production
npm run start        # Start production server
npm run lint         # Run ESLint

# Database
npx prisma studio    # Open Prisma Studio
npx prisma migrate dev # Create new migration
npx prisma generate  # Generate Prisma Client

Deployment

Vercel (Recommended)

  1. Push your code to GitHub
  2. Import project in Vercel
  3. Add environment variables
  4. Deploy

Other Platforms

This app can be deployed to any platform that supports Next.js:

  • AWS
  • Google Cloud
  • Railway
  • Render
  • DigitalOcean

Make sure to:

  • Set all environment variables
  • Run database migrations
  • Configure OAuth callback URLs

πŸ”’ Security Best Practices

  • βœ… Passwords hashed with bcrypt
  • βœ… CSRF protection enabled
  • βœ… Secure session management
  • βœ… Input validation and sanitization
  • βœ… Rate limiting on auth endpoints
  • βœ… Environment variables for secrets

🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

πŸ”— Links


Made with ❀️ using Next.js and Better Auth

About

A modern, full-featured authentication system built with Next.js 15, Better Auth, Prisma, and TypeScript.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors