Skip to content

TheoNolasco/backend-better-auth-nodejs

Repository files navigation

Backend API Server

A Node.js Express API server with authentication, protected routes, and PostgreSQL database integration.

Tech Stack

  • Node.js with Express.js
  • Better Auth for authentication (email/password)
  • PostgreSQL database with Drizzle ORM
  • CORS configured for frontend integration
  • Docker support for database setup

Project Structure

├── auth/                    # Authentication configuration
├── data/                    # Mock data (JSON files)
├── db/                      # Database schema and utilities
├── docker/                  # Docker setup for PostgreSQL
├── routes/                  # API routes
├── utils/                   # Utility functions and middleware
├── index.js                 # Main server file
├── package.json             # Dependencies and scripts
└── .env.example             # Environment variables template

Quick Start

Prerequisites

  • Node.js (v18 or higher)
  • Docker and Docker Compose

1. Install Dependencies

npm install

2. Environment Setup

cp .env.example .env
# Edit .env if needed (defaults should work for development)

3. Database Setup

cd docker && docker compose up -d && cd ..
# This will start PostgreSQL with the default configuration

Or from npm script:

npm run db:up

More commands are available in package.json for managing the docker setup. For detailed database setup instructions, see docker/README.md.

Database Management

The Docker setup includes Adminer web interface for database management at http://localhost:8080.

Use it to browse Better Auth tables, run queries, and debug authentication. See docker/README.md for login credentials and detailed usage.

4. Start Development Server

npm run dev

The API server will be available at: http://localhost:3001

Features

Authentication

  • User registration and login
  • Email/password authentication
  • Session-based authentication with cookies
  • Protected routes and API endpoints
  • Automatic session management

Properties System

  • Authenticated property listings
  • Individual property detail views
  • Modal-based property details
  • Mock data system with JSON files

Development Features

  • Comprehensive logging for authentication flows
  • CORS configured for frontend integration
  • Hot reloading during development
  • Environment-based configuration

API Endpoints

Authentication

  • POST /api/auth/sign-up/email - User registration
  • POST /api/auth/sign-in/email - User login
  • POST /api/auth/sign-out - User logout
  • GET /api/auth/get-session - Get current session

Properties (Protected)

  • GET /api/properties - Get all properties
  • GET /api/properties/:id - Get single property details

System

  • GET /api/status/health - Health check
  • GET /api/status/info - Server information

Data Management

Mock data is stored in JSON files for easy management:

  • data/properties.json - Basic property listings
  • data/property-details.json - Detailed property information

You can edit these files directly to modify the available properties without touching the code.

Testing with curl

Create a test user

curl -X POST http://localhost:3001/api/auth/sign-up/email \
  -H "Content-Type: application/json" \
  -H "Origin: http://localhost:3000" \
  -d '{
    "email": "test@example.com",
    "password": "password123",
    "name": "Test User"
  }'

Sign in with the test user

curl -X POST http://localhost:3001/api/auth/sign-in/email \
  -H "Content-Type: application/json" \
  -H "Origin: http://localhost:3000" \
  -d '{
    "email": "test@example.com",
    "password": "password123"
  }'

Check server health

curl http://localhost:3001/api/status/health

Development

Adding New Protected Endpoints

// In routes/index.js
app.get('/api/your-endpoint', protectedEndpointLogging, requireAuth, yourHandler);

Authentication Middleware

The requireAuth middleware is available from auth/auth.js and automatically:

  • Verifies user sessions
  • Adds user data to req.user
  • Returns 401 for unauthenticated requests

Frontend Integration

For frontend applications, make authenticated requests with:

fetch('http://localhost:3001/api/endpoint', {
  credentials: 'include', // Important for cookie auth
});

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors