Skip to content

skaldlabs/auth-chat-example

Repository files navigation

Chat RH - AI-Powered Chat System

A complete monorepo chat system with admin panel, built with React, Express, TypeScript, and powered by Skald AI.

Features

  • Authentication: Secure login/logout with httpOnly cookies
  • Chat Interface: ChatGPT-like streaming chat with conversation history
  • Admin Panel: User management with create, delete, and password reset
  • Responsive Design: Works seamlessly on desktop and mobile
  • Containerized: Full Docker support for easy deployment

Tech Stack

Frontend

  • React 18 + TypeScript
  • Vite
  • TailwindCSS + shadcn/ui
  • React Router
  • Server-Sent Events (SSE) for streaming

Backend

  • Express + TypeScript
  • MikroORM (PostgreSQL)
  • express-session with PostgreSQL store
  • Skald AI SDK
  • bcrypt for password hashing

Infrastructure

  • pnpm workspaces (monorepo)
  • PostgreSQL 16
  • Docker + docker-compose
  • nginx (production frontend)

Project Structure

/
├── packages/
│   ├── backend/         # Express API
│   ├── frontend/        # React app
│   └── shared/          # Shared TypeScript types
├── docker-compose.yml
├── pnpm-workspace.yaml
└── .env

Quick Start

Prerequisites

  • Node.js 18+
  • pnpm 8+
  • Docker & Docker Compose (for PostgreSQL)

Development Setup (Recommended)

  1. Clone and install dependencies
cd chat-rh
cp .env.example .env
pnpm install
  1. Configure environment variables

Edit .env and add your Skald API key:

SKALD_API_KEY=sk_proj_your_actual_key_here
  1. Start PostgreSQL
docker-compose up postgres -d
  1. Build shared package
cd packages/shared
pnpm build
cd ../..
  1. Run database migrations
cd packages/backend
pnpm migration:up
  1. Create an admin user
cd packages/backend
pnpm create-admin

Follow the prompts to create your admin account.

  1. Start development servers

In the root directory:

pnpm dev

This will start:

Your system is now running! Open http://localhost:5173 in your browser.

Production Deployment with Docker

Note: Docker deployment currently has issues with bcrypt native module compilation in Alpine Linux. For production, we recommend deploying the backend and frontend separately or using a Debian-based image. Development mode (above) works perfectly.

Alternative: Run Production Build Locally

  1. Build all packages
pnpm build
  1. Start PostgreSQL
docker-compose up postgres -d
  1. Run migrations
cd packages/backend
pnpm migration:up
  1. Start the backend
cd packages/backend
pnpm start
  1. Serve the frontend

Use any static file server:

cd packages/frontend
npx serve dist

The application will be available at the configured ports.

Usage

Regular Users

  1. Login: Use credentials provided by admin
  2. First Login: You'll be prompted to change password
  3. Chat: Start conversations with AI assistant
  4. History: All conversations are persisted in the database

Admin Users

  1. Login: Use admin credentials
  2. Access Admin Panel: Click "Admin Panel" button
  3. User Management:
    • Create new users (auto-generates temporary password)
    • Delete users
    • Reset user passwords
  4. Chat: Admins can also use the chat interface

API Endpoints

Authentication

  • POST /api/auth/login - Login
  • POST /api/auth/logout - Logout
  • GET /api/auth/me - Get current user
  • POST /api/auth/change-password - Change password

Admin

  • GET /api/admin/users - List all users
  • POST /api/admin/users - Create user
  • DELETE /api/admin/users/:id - Delete user
  • POST /api/admin/users/:id/reset-password - Reset password

Chat

  • GET /api/chats - Get user's chats
  • GET /api/chats/:id - Get chat with messages
  • DELETE /api/chats/:id - Delete chat
  • POST /api/stream - Stream chat messages (SSE)

Development Commands

Root

pnpm dev          # Start all packages in dev mode
pnpm build        # Build all packages
pnpm clean        # Clean all dist folders
pnpm type-check   # Type check all packages

Backend

pnpm dev                 # Start dev server
pnpm build              # Build for production
pnpm create-admin       # Create admin user
pnpm migration:create   # Create new migration
pnpm migration:up       # Run migrations

Frontend

pnpm dev      # Start dev server
pnpm build    # Build for production
pnpm preview  # Preview production build

Environment Variables

Backend

  • DATABASE_URL - PostgreSQL connection string
  • DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD - Database config
  • PORT - Backend port (default: 3001)
  • NODE_ENV - Environment (development/production)
  • SESSION_SECRET - Session encryption key
  • SKALD_API_KEY - Skald AI API key

Frontend

  • VITE_API_URL - Backend API URL

Architecture Decisions

Session Management

  • Uses PostgreSQL-backed sessions for reliability
  • httpOnly cookies for security
  • 7-day session lifetime

Chat Context

  • Conversation history stored in database
  • Context included in system prompt for continuity
  • New chat_id generated on page refresh (per requirements)

Streaming

  • Server-Sent Events (SSE) for real-time responses
  • Chunked token delivery for smooth UX
  • Automatic reconnection handling

Security

  • Password hashing with bcrypt
  • Role-based access control (user/admin)
  • Forced password change on first login
  • CORS configured for credentials

Troubleshooting

Database Connection Issues

# Check if PostgreSQL is running
docker ps

# View logs
docker logs chatrh-postgres

Backend Issues

# View backend logs
docker logs chatrh-backend

# Restart backend
docker-compose restart backend

Frontend Issues

# Clear browser cache
# Check browser console for errors
# Verify API proxy in vite.config.ts

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published