Skip to main content

Authentication Setup Guide

Complete guide for setting up OAuth authentication with HuggingFace, Google, Facebook, and GitHub, plus Neon serverless PostgreSQL.

๐Ÿฅ‡ Database Setup: Neon (Serverless PostgreSQL)โ€‹

Recommended Choice - Free tier, zero-config, perfect for production.

Why Neon?โ€‹

โœ… Free tier: 0.5 GB storage with scale-to-zero
โœ… Managed backups: Point-in-time recovery included
โœ… Encrypted: At rest + in transit
โœ… Public internet: Perfect for HuggingFace Spaces
โœ… Standard PostgreSQL: No vendor lock-in
โœ… Enterprise backing: Acquired by Databricks (2025)

Setup Stepsโ€‹

  1. Sign up at neon.tech

    • Click "Sign up" (free, no credit card required)
    • Sign in with GitHub or email
  2. Create a new project

    • Click "New Project"
    • Name: open-navigator-engagement
    • Region: Choose closest to your users (e.g., US East)
    • PostgreSQL version: 16 (latest)
  3. Copy connection string

    • Go to Dashboard โ†’ Connection Details
    • Copy the "Connection string"
    • Format: postgresql://user:password@ep-xxx.us-east-2.aws.neon.tech/neondb?sslmode=require
  4. Add to .env file

    DATABASE_URL=postgresql://user:password@ep-xxx.us-east-2.aws.neon.tech/neondb?sslmode=require
  5. Database auto-initialization

    • Tables are created automatically on first API startup
    • No migration scripts needed!

Verify Connectionโ€‹

# Start the API server
source .venv/bin/activate
python main.py serve

# You should see:
# โœ… Database initialized at: postgresql://...

๐Ÿ” OAuth Provider Setupโ€‹

1. HuggingFace OAuthโ€‹

Get credentials: huggingface.co/settings/applications

Steps:โ€‹

  1. Go to HuggingFace Settings โ†’ Applications
  2. Click "Create an OAuth app"
  3. Fill in:
    • Application name: Open Navigator
    • Homepage URL: https://www.communityone.com
    • Redirect URI:
      • Development: http://localhost:8000/auth/callback/huggingface
      • Production: https://www.communityone.com/api/auth/callback/huggingface
    • Scopes: openid profile email
  4. Click "Create"
  5. Copy Client ID and Client Secret

Add to .env:โ€‹

HUGGINGFACE_CLIENT_ID=hf_oauth_xxx
HUGGINGFACE_CLIENT_SECRET=hf_oauth_secret_xxx

2. Google OAuthโ€‹

Get credentials: console.cloud.google.com/apis/credentials

Steps:โ€‹

  1. Create a project (or select existing)

    • Go to Google Cloud Console
    • Select project or click "New Project"
    • Name: Open Navigator
  2. Enable Google+ API

    • Go to APIs & Services โ†’ Library
    • Search "Google+ API"
    • Click Enable
  3. Configure OAuth consent screen

    • Go to APIs & Services โ†’ OAuth consent screen
    • User Type: External
    • App name: Open Navigator
    • User support email: Your email
    • Developer contact: Your email
    • Scopes: email, profile, openid
  4. Create OAuth 2.0 Client ID

    • Go to APIs & Services โ†’ Credentials
    • Click "Create Credentials" โ†’ OAuth client ID
    • Application type: Web application
    • Name: Open Navigator Web Client
    • Authorized redirect URIs:
      • http://localhost:8000/auth/callback/google (development)
      • https://www.communityone.com/api/auth/callback/google (production)
    • Click "Create"
  5. Copy Client ID and Client Secret

Add to .env:โ€‹

GOOGLE_CLIENT_ID=123456789-xxxxx.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=GOCSPX-xxxxx

3. Facebook OAuthโ€‹

Get credentials: developers.facebook.com/apps

Steps:โ€‹

  1. Create a new app

    • Click "Create App"
    • Type: Consumer
    • App Name: Open Navigator
  2. Add Facebook Login

    • Dashboard โ†’ Add Product
    • Select "Facebook Login" โ†’ Set up
  3. Configure OAuth settings

    • Go to Facebook Login โ†’ Settings
    • Valid OAuth Redirect URIs:
      • http://localhost:8000/auth/callback/facebook
      • https://www.communityone.com/api/auth/callback/facebook
    • Client OAuth Login: Yes
    • Web OAuth Login: Yes
  4. Get App ID and Secret

    • Go to Settings โ†’ Basic
    • Copy App ID and App Secret

Add to .env:โ€‹

FACEBOOK_APP_ID=1234567890123456
FACEBOOK_APP_SECRET=xxxxxxxxxxxxx

4. GitHub OAuthโ€‹

Get credentials: github.com/settings/developers

Steps:โ€‹

  1. Register new OAuth application

    • Go to Settings โ†’ Developer settings โ†’ OAuth Apps
    • Click "New OAuth App"
  2. Fill in details

    • Application name: Open Navigator
    • Homepage URL: https://www.communityone.com
    • Authorization callback URL:
      • Development: http://localhost:8000/auth/callback/github
      • Production: https://www.communityone.com/api/auth/callback/github
  3. Create application

    • Click "Register application"
    • Copy Client ID
    • Generate Client Secret and copy it

Add to .env:โ€‹

GITHUB_CLIENT_ID=Iv1.xxxxxxxxxxxxx
GITHUB_CLIENT_SECRET=xxxxxxxxxxxxx

๐Ÿ”‘ Generate JWT Secretโ€‹

Create a secure random secret for JWT tokens:

# Generate 32-byte random secret
openssl rand -hex 32

# Or use Python
python -c "import secrets; print(secrets.token_urlsafe(32))"

Add to .env:

JWT_SECRET_KEY=your_random_32_char_secret_here

๐ŸŒ Environment Configurationโ€‹

Development .env:โ€‹

# Database
DATABASE_URL=postgresql://user:password@ep-xxx.us-east-2.aws.neon.tech/neondb?sslmode=require

# JWT
JWT_SECRET_KEY=your_random_secret_key

# Frontend URL
FRONTEND_URL=http://localhost:5173

# OAuth (all providers)
HUGGINGFACE_CLIENT_ID=hf_oauth_xxx
HUGGINGFACE_CLIENT_SECRET=hf_oauth_secret_xxx
GOOGLE_CLIENT_ID=123456789-xxx.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=GOCSPX-xxx
FACEBOOK_APP_ID=123456789
FACEBOOK_APP_SECRET=xxx
GITHUB_CLIENT_ID=Iv1.xxx
GITHUB_CLIENT_SECRET=xxx

Production (HuggingFace Spaces)โ€‹

Add secrets in Space Settings โ†’ Repository secrets:

Secret NameValue
DATABASE_URLpostgresql://...neon.tech/...
JWT_SECRET_KEYYour random secret
FRONTEND_URLhttps://www.communityone.com
HUGGINGFACE_CLIENT_IDFrom HF OAuth app
HUGGINGFACE_CLIENT_SECRETFrom HF OAuth app
GOOGLE_CLIENT_IDFrom Google Cloud
GOOGLE_CLIENT_SECRETFrom Google Cloud
FACEBOOK_APP_IDFrom Facebook app
FACEBOOK_APP_SECRETFrom Facebook app
GITHUB_CLIENT_IDFrom GitHub OAuth app
GITHUB_CLIENT_SECRETFrom GitHub OAuth app

๐Ÿงช Testing Authenticationโ€‹

1. Start the API serverโ€‹

source .venv/bin/activate
python main.py serve

2. Start the frontendโ€‹

cd frontend
npm run dev

3. Test OAuth flowsโ€‹

  1. Visit http://localhost:5173
  2. Click "Login" in top-right
  3. Select a provider (HuggingFace, Google, Facebook, or GitHub)
  4. Complete OAuth flow
  5. You should be redirected back with your profile visible

4. Verify databaseโ€‹

Check that user was created in Neon:

# Option 1: Neon SQL Editor (in dashboard)
SELECT * FROM users;

# Option 2: psql client
psql "postgresql://user:password@ep-xxx.neon.tech/neondb?sslmode=require"
\dt # List tables
SELECT * FROM users;

๐Ÿ“Š Database Schemaโ€‹

The authentication system creates these tables automatically:

users tableโ€‹

ColumnTypeDescription
idIntegerPrimary key
emailString(255)User email (unique)
usernameString(100)Optional username
full_nameString(255)Display name
avatar_urlString(500)Profile picture URL
oauth_providerString(50)huggingface, google, facebook, github
oauth_idString(255)Provider's user ID
hashed_passwordString(255)For email/password (optional)
is_activeBooleanAccount status
is_verifiedBooleanEmail verification status
created_atDateTimeAccount creation
updated_atDateTimeLast update
last_loginDateTimeLast login timestamp
preferencesTextUser settings (JSON)

oauth_states tableโ€‹

ColumnTypeDescription
idIntegerPrimary key
state_tokenString(255)CSRF protection token
providerString(50)OAuth provider name
redirect_uriString(500)Callback URL
created_atDateTimeCreation timestamp
expires_atDateTimeExpiration (10 minutes)

๐Ÿ”’ Security Best Practicesโ€‹

โœ… DO:โ€‹

  • Use HTTPS in production
  • Rotate JWT secrets regularly
  • Keep OAuth secrets in environment variables (never commit to git)
  • Use Neon's connection pooling
  • Enable Neon's IP allowlist for production

โŒ DON'T:โ€‹

  • Commit .env file to git (it's in .gitignore)
  • Share OAuth secrets publicly
  • Use weak JWT secrets
  • Disable SSL mode on Neon connections

๐Ÿš€ Production Deploymentโ€‹

HuggingFace Spacesโ€‹

All environment variables are automatically loaded from Repository secrets.

Update OAuth redirect URIsโ€‹

After deploying, update all OAuth apps with production callback URLs:

  • HuggingFace: https://www.communityone.com/api/auth/callback/huggingface
  • Google: https://www.communityone.com/api/auth/callback/google
  • Facebook: https://www.communityone.com/api/auth/callback/facebook
  • GitHub: https://www.communityone.com/api/auth/callback/github

๐Ÿ› Troubleshootingโ€‹

Database connection failsโ€‹

โŒ could not connect to server

Fix:

  • Verify DATABASE_URL is correct
  • Check Neon project is not suspended (free tier auto-suspends after inactivity)
  • Ensure ?sslmode=require is in connection string

OAuth redirect mismatchโ€‹

โŒ redirect_uri_mismatch

Fix:

  • Check redirect URI in OAuth app settings matches your server
  • Ensure http:// vs https:// matches
  • Verify port number (:8000 for API)

JWT token invalidโ€‹

โŒ Could not validate credentials

Fix:

  • Ensure JWT_SECRET_KEY is set and matches between sessions
  • Check token hasn't expired (7-day default)
  • Clear browser localStorage and re-login

Tables not createdโ€‹

โŒ relation "users" does not exist

Fix:

  • Restart API server to trigger init_db()
  • Check database connection is successful
  • Manually run: from api.database import init_db; init_db()

๐Ÿ“š Additional Resourcesโ€‹


โœ… Checklistโ€‹

Before going live, ensure:

  • Neon database created and connected
  • All 4 OAuth apps configured with production redirect URIs
  • JWT secret generated (32+ characters)
  • Environment variables added to HuggingFace Spaces secrets
  • Database tables created (users, oauth_states)
  • OAuth flows tested with all 4 providers
  • HTTPS enabled on custom domain
  • Neon IP allowlist configured (optional, for extra security)

Need help? Open an issue on GitHub