Skip to main content

Authentication Overview

The JOIP API uses session-based authentication with HTTP-only cookies for secure, stateless user sessions.

Authentication Methods

JOIP supports two authentication strategies based on the deployment environment:

1. Local Development (Default)

For local development and testing, the API uses Passport.js with LocalStrategy.

Test Accounts

Three test accounts are available in development:

Login Request

Response: 302 Redirect with session cookie

2. Replit OIDC (Production)

When deployed on Replit, the API automatically uses Replit’s OIDC authentication. Detection based on environment variable:

Session Configuration

Session Storage

Sessions are stored in PostgreSQL using connect-pg-simple:
  • httpOnly: true - Prevents JavaScript access (XSS protection)
  • secure: true in production - HTTPS only
  • maxAge: 7 days (604,800,000 ms)
  • sameSite: lax - CSRF protection

Authentication Endpoints

POST /api/login

Authenticate and receive session cookie. Request:
Success Response: 302 Redirect to / Error Response:
Rate Limit: 5 attempts per 15 minutes per IP (failed attempts only)

GET /api/login

Client-side redirect endpoint (auto-login in development).

GET /api/dev-login

Development-only endpoint for quick auto-login. Response: 302 Redirect to / with dev account session

GET /api/logout

Terminate current session. Response: 302 Redirect to /

POST /api/auth/logout

API-compatible logout endpoint. Response:

GET /api/auth/user

Get current authenticated user details. Request:
Response:
Error Response (401):

Authentication Middleware

Protected endpoints use the isAuthenticated middleware:

User ID Extraction

The API uses a consistent method to extract user IDs from requests:

Making Authenticated Requests

Using cURL

  1. Login and save cookies:
  1. Use cookies in subsequent requests:

Using JavaScript (fetch)

Using Python (requests)

Session Lifecycle

User Creation Flow

When a user logs in for the first time:

Activity Tracking

Successful logins are tracked:

Security Best Practices

Password Security

In production, passwords should be hashed using bcrypt. The current implementation uses plaintext passwords for development testing only.

Environment Variables

Required for authentication:

CSRF Protection

Sessions use sameSite: 'lax' cookie policy:
  • Prevents CSRF attacks from external sites
  • Allows cookies on top-level GET navigations
  • Blocks cookies on cross-site POST requests

XSS Protection

  • httpOnly cookies: Session cookies cannot be accessed via JavaScript
  • Input sanitization: All user inputs are sanitized before storage
  • Output encoding: Data is properly encoded in responses

Common Authentication Errors

401 Unauthorized

Cause: Missing or invalid session cookie Solution:
  1. Ensure you’re logged in
  2. Include credentials: 'include' in fetch requests
  3. Check cookie expiration (7 days)

403 Forbidden

Cause: Authenticated but lacking required permissions Solution: Check user role and endpoint access requirements

Invalid credentials

Cause: Wrong email or password Solution: Verify credentials match test accounts

Next Steps