← back to Letsbegin

lib/auth.ts

38 lines

/**
 * Authentication System for Letsbegin
 * Uses same credentials as DW-Agents for consistency
 */

export const AUTH_CONFIG = {
  username: 'admin',
  password: requireEnv('AUTH_PASSWORD'),
  sessionName: 'letsbegin_session',
  sessionSecret: requireEnv('SESSION_SECRET'),
  cookieMaxAge: 30 * 24 * 60 * 60 * 1000, // 30 days
}

function requireEnv(name: string): string {
  const value = process.env[name]
  if (!value) {
    throw new Error(`${name} environment variable is required`)
  }
  return value
}

// IPs that bypass authentication (localhost, server itself)
export const WHITELISTED_IPS = ['127.0.0.1', '::1', '45.61.58.125', 'localhost']

export interface AuthSession {
  authenticated: boolean
  username?: string
  loginTime?: number
}

export function validateCredentials(username: string, password: string): boolean {
  return username === AUTH_CONFIG.username && password === AUTH_CONFIG.password
}

export function isWhitelistedIP(ip: string): boolean {
  return WHITELISTED_IPS.includes(ip)
}