An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: Make authentication optional with anonymous mode by default
Yoshiki Miuraca2a9517c4e3
diff --git a/.env.local.example b/.env.local.exampleindex 5d870f1b..ea3fda30 100644--- a/.env.local.example+++ b/.env.local.example@@ -76,20 +76,29 @@ TAVILY_API_KEY=[YOUR_TAVILY_API_KEY]# Note: Can be made optional by commenting out after initial setupBRAVE_SEARCH_API_KEY=[YOUR_BRAVE_SEARCH_API_KEY]-# ------------------------------------------------------------------------------# Authentication Configuration (Required)-# ------------------------------------------------------------------------------# Supabase Authentication-# Get your project URL and anon key from: https://supabase.com/dashboard-# Note: Can be made optional in future releases for anonymous-only usage-NEXT_PUBLIC_SUPABASE_URL=[YOUR_SUPABASE_PROJECT_URL]-NEXT_PUBLIC_SUPABASE_ANON_KEY=[YOUR_SUPABASE_ANON_KEY]-# =============================================================================# OPTIONAL FEATURES# Enable additional functionality as needed# =============================================================================+# -----------------------------------------------------------------------------+# Authentication Configuration (Optional)+# -----------------------------------------------------------------------------+# By default, authentication is disabled for personal use (ENABLE_AUTH=false)+# All users share the same anonymous user ID+# ⚠️ For personal, single-user environments only++# Default: Authentication disabled (anonymous mode)+ENABLE_AUTH=false+ANONYMOUS_USER_ID=anonymous-user++# To enable Supabase authentication, set ENABLE_AUTH=true and configure:+# Get your project URL and anon key from: https://supabase.com/dashboard+# ENABLE_AUTH=true+# NEXT_PUBLIC_SUPABASE_URL=[YOUR_SUPABASE_PROJECT_URL]+# NEXT_PUBLIC_SUPABASE_ANON_KEY=[YOUR_SUPABASE_ANON_KEY]+# =============================================================================+# -----------------------------------------------------------------------------# Cloud Deployment Configuration# -----------------------------------------------------------------------------@@ -163,6 +172,18 @@ NEXT_PUBLIC_SUPABASE_ANON_KEY=[YOUR_SUPABASE_ANON_KEY]# POSTGRES_DB=morphic # Default: morphic# POSTGRES_PORT=5432 # Default: 5432+# Redis Configuration (Optional)+# Local Redis is used by default for SearXNG advanced search caching+# Only set this if you need a custom Redis URL+# Default: redis://localhost:6379 (automatically used in Docker)+# LOCAL_REDIS_URL=redis://localhost:6379++# Database SSL Configuration (Optional)+# Set to true to disable SSL certificate verification+# Automatically set to true in docker-compose.yaml for local PostgreSQL+# IMPORTANT: Do NOT set this for cloud databases (Neon, Supabase, etc.)+# DATABASE_SSL_DISABLED=true+# -----------------------------------------------------------------------------# Advanced Configuration# -----------------------------------------------------------------------------diff --git a/CLAUDE.md b/CLAUDE.mdindex 5b204864..ecf86dff 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -24,6 +24,31 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co- `docker compose down -v` - Stop all containers and remove volumes (deletes database data)- `docker pull ghcr.io/miurla/morphic:latest` - Pull prebuilt Docker image+#### Docker Authentication++**Default Behavior**: Docker deployments run in **anonymous mode** (authentication disabled).++When running with Docker Compose, `ENABLE_AUTH=false` is set by default, allowing personal use without Supabase setup. All users share a single anonymous user ID.++**⚠️ Security Warning:**+- Anonymous mode is **only for personal, single-user local environments**+- All chat history is shared under one user ID+- **NOT suitable** for multi-user or production deployments+- Morphic Cloud deployments block `ENABLE_AUTH=false` automatically++**Enabling Authentication:**+To require Supabase authentication, set:+```bash+ENABLE_AUTH=true # or remove ENABLE_AUTH from docker-compose.yaml+NEXT_PUBLIC_SUPABASE_URL=[your-supabase-url]+NEXT_PUBLIC_SUPABASE_ANON_KEY=[your-supabase-anon-key]+```++**Implementation:**+- Auth logic: [lib/auth/get-current-user.ts:22-40](lib/auth/get-current-user.ts#L22-L40)+- Always warns when `ENABLE_AUTH=false` (except in tests)+- Guards against `MORPHIC_CLOUD_DEPLOYMENT=true`+## Architecture Overview### Tech Stackdiff --git a/README.md b/README.mdindex 3120c939..b3aec446 100644--- a/README.md+++ b/README.md@@ -173,16 +173,15 @@ cp .env.local.example .env.localFill in the required environment variables in `.env.local`:```bash-# Required Configuration-DATABASE_URL= # PostgreSQL connection string-OPENAI_API_KEY= # Get from https://platform.openai.com/api-keys-TAVILY_API_KEY= # Get from https://app.tavily.com/home-BRAVE_SEARCH_API_KEY= # Get from https://brave.com/search/api/-NEXT_PUBLIC_SUPABASE_URL= # Your Supabase project URL-NEXT_PUBLIC_SUPABASE_ANON_KEY= # Your Supabase anonymous key+DATABASE_URL=your_database_url+OPENAI_API_KEY=your_openai_key+TAVILY_API_KEY=your_tavily_key+BRAVE_SEARCH_API_KEY=your_brave_key```-For optional features configuration (SearXNG, alternative AI providers, etc.), see [CONFIGURATION.md](./docs/CONFIGURATION.md)+**Note**: Authentication is disabled by default (`ENABLE_AUTH=false`).++For optional features (authentication, SearXNG, alternative AI providers, etc.), see [CONFIGURATION.md](./docs/CONFIGURATION.md)### 4. Run database migrations@@ -208,20 +207,17 @@ bun devcp .env.local.example .env.local```-Edit `.env.local` and set the DATABASE_URL to use the Docker PostgreSQL service:+Edit `.env.local` and set the required variables:```bash-# Database Configuration for DockerDATABASE_URL=postgresql://morphic:morphic@postgres:5432/morphic--# Required API Keys-OPENAI_API_KEY= # Get from https://platform.openai.com/api-keys-TAVILY_API_KEY= # Get from https://app.tavily.com/home-BRAVE_SEARCH_API_KEY= # Get from https://brave.com/search/api/-NEXT_PUBLIC_SUPABASE_URL= # Your Supabase project URL-NEXT_PUBLIC_SUPABASE_ANON_KEY= # Your Supabase anonymous key+OPENAI_API_KEY=your_openai_key+TAVILY_API_KEY=your_tavily_key+BRAVE_SEARCH_API_KEY=your_brave_key```+**Note**: Authentication is disabled by default (`ENABLE_AUTH=false` in `.env.local.example`).+**Optional**: Customize PostgreSQL credentials by setting environment variables in `.env.local`:```bashdiff --git a/app/api/advanced-search/route.ts b/app/api/advanced-search/route.tsindex a53addd5..90dedf46 100644--- a/app/api/advanced-search/route.ts+++ b/app/api/advanced-search/route.ts@@ -33,23 +33,31 @@ let redisClient: Redis | ReturnType<typeof createClient> | null = nullasync function initializeRedisClient() {if (redisClient) return redisClient- const useLocalRedis = process.env.USE_LOCAL_REDIS === 'true'+ const upstashRedisRestUrl = process.env.UPSTASH_REDIS_REST_URL+ const upstashRedisRestToken = process.env.UPSTASH_REDIS_REST_TOKEN++ // Use Upstash Redis if credentials are provided+ if (upstashRedisRestUrl && upstashRedisRestToken) {+ redisClient = new Redis({+ url: upstashRedisRestUrl,+ token: upstashRedisRestToken+ })+ return redisClient+ }- if (useLocalRedis) {+ // Otherwise, try to use local Redis (for Docker/SearXNG usage)+ try {const localRedisUrl =process.env.LOCAL_REDIS_URL || 'redis://localhost:6379'- redisClient = createClient({ url: localRedisUrl })- await redisClient.connect()- } else {- const upstashRedisRestUrl = process.env.UPSTASH_REDIS_REST_URL- const upstashRedisRestToken = process.env.UPSTASH_REDIS_REST_TOKEN-- if (upstashRedisRestUrl && upstashRedisRestToken) {- redisClient = new Redis({- url: upstashRedisRestUrl,- token: upstashRedisRestToken- })- }+ const client = createClient({ url: localRedisUrl })+ await client.connect()+ redisClient = client+ } catch (error) {+ console.warn(+ 'Failed to connect to local Redis. Advanced search caching disabled.',+ error+ )+ redisClient = null}return redisClientdiff --git a/docker-compose.yaml b/docker-compose.yamlindex 019d2d41..ed20046e 100644--- a/docker-compose.yaml+++ b/docker-compose.yaml@@ -13,10 +13,12 @@ services:- morphic:latestenv_file: .env.local # Load environment variablesenvironment:- # Set NODE_ENV to development for local Docker environment- NODE_ENV: development- # Override DATABASE_URL for Docker Compose (without sslmode, handled by NODE_ENV)+ # Database connection for Docker ComposeDATABASE_URL: postgresql://${POSTGRES_USER:-morphic}:${POSTGRES_PASSWORD:-morphic}@postgres:5432/${POSTGRES_DB:-morphic}+ DATABASE_SSL_DISABLED: 'true' # Disable SSL for local Docker PostgreSQL+ # Authentication settings (default: disabled for personal Docker use)+ ENABLE_AUTH: ${ENABLE_AUTH:-false}+ ANONYMOUS_USER_ID: ${ANONYMOUS_USER_ID:-docker-anonymous}ports:- '3000:3000' # Maps port 3000 on the host to port 3000 in the container.depends_on:diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.mdindex 2cbbdb45..e1728a31 100644--- a/docs/CONFIGURATION.md+++ b/docs/CONFIGURATION.md@@ -4,10 +4,46 @@ This guide covers the optional features and their configuration in Morphic.## Table of Contents+- [Authentication](#authentication)- [Search Providers](#search-providers)- [Additional AI Providers](#additional-ai-providers)- [Other Features](#other-features)+## Authentication++By default, Morphic runs in **anonymous mode** with authentication disabled (`ENABLE_AUTH=false` in `.env.local.example`). This is ideal for personal use where all users share a single anonymous user ID.++### Anonymous Mode (Default)++No configuration needed. The default settings in `.env.local.example` include:++```bash+ENABLE_AUTH=false+ANONYMOUS_USER_ID=anonymous-user+```++**⚠️ Important**: Anonymous mode is only suitable for personal, single-user environments. All chat history is shared under one user ID.++### Enabling Supabase Authentication++To enable user authentication with Supabase:++1. Create a Supabase project at [supabase.com](https://supabase.com)++2. Set the following environment variables in `.env.local`:++```bash+ENABLE_AUTH=true+NEXT_PUBLIC_SUPABASE_URL=[YOUR_SUPABASE_PROJECT_URL]+NEXT_PUBLIC_SUPABASE_ANON_KEY=[YOUR_SUPABASE_ANON_KEY]+```++3. Obtain your credentials from the Supabase dashboard:+ - **Project URL**: Settings → API → Project URL+ - **Anon Key**: Settings → API → Project API keys → anon/public++With authentication enabled, users will need to sign up/login to use Morphic, and each user will have isolated chat history.+## Search Providers### SearXNG Configurationdiff --git a/lib/auth/get-current-user.ts b/lib/auth/get-current-user.tsindex 78573247..ad48b90b 100644--- a/lib/auth/get-current-user.ts+++ b/lib/auth/get-current-user.ts@@ -18,6 +18,27 @@ export async function getCurrentUser() {export async function getCurrentUserId() {const count = incrementAuthCallCount()perfLog(`getCurrentUserId called - count: ${count}`)++ // Skip authentication mode (for personal Docker deployments)+ if (process.env.ENABLE_AUTH === 'false') {+ // Guard: Prevent disabling auth in Morphic Cloud deployments+ if (process.env.MORPHIC_CLOUD_DEPLOYMENT === 'true') {+ throw new Error(+ 'ENABLE_AUTH=false is not allowed in MORPHIC_CLOUD_DEPLOYMENT'+ )+ }++ // Always warn when authentication is disabled (except in tests)+ if (process.env.NODE_ENV !== 'test') {+ console.warn(+ '⚠️ Authentication disabled. Running in anonymous mode.\n' ++ ' All users share the same user ID. For personal use only.'+ )+ }++ return process.env.ANONYMOUS_USER_ID || 'anonymous-user'+ }+const user = await getCurrentUser()return user?.id}diff --git a/lib/db/index.ts b/lib/db/index.tsindex dc289de9..1aef2040 100644--- a/lib/db/index.ts+++ b/lib/db/index.ts@@ -42,8 +42,17 @@ if (isDevelopment) {: 'Owner User (RLS Bypassed)')}++// SSL configuration: Use environment variable to control SSL+// DATABASE_SSL_DISABLED=true disables SSL completely (for local/Docker PostgreSQL)+// Default is to enable SSL with certificate verification (for cloud databases like Neon, Supabase)+const sslConfig =+ process.env.DATABASE_SSL_DISABLED === 'true'+ ? false // Disable SSL entirely for local PostgreSQL+ : { rejectUnauthorized: true } // Enable SSL with verification for cloud DBs+const client = postgres(connectionString, {- ssl: { rejectUnauthorized: false },+ ssl: sslConfig,prepare: false,max: 20 // Max 20 connections})