Overview
DelightBridge uses NextAuth.js with Google OAuth for authentication. All API routes are protected by session-based authentication, meaning users must be logged in via the web interface before making API requests.DelightBridge does not currently support API keys or bearer tokens. All requests must include a valid session cookie.
Authentication Flow
User logs in with Google
Users authenticate via Google OAuth through the
/login page. NextAuth handles the OAuth flow and creates a session.Session cookie is set
After successful login, NextAuth sets a session cookie named
authjs.session-token (or __Secure-authjs.session-token in production with HTTPS).Cookie is sent with requests
The browser automatically includes this cookie with all subsequent API requests to the same domain.
Session Validation
The authentication logic is implemented in/src/lib/session.ts:
session.ts
Usage in API Routes
Every protected endpoint uses this pattern:route.ts
Permission Levels
DelightBridge implements a hierarchical permission system with four levels:view
Can view threads and services (read-only)
edit
Can modify drafts and service configurations
send
Can send email responses to customers
admin
Can manage members and all workspace settings
Permission Hierarchy
Permissions follow a hierarchy where higher levels inherit lower level permissions:- admin can do everything (view + edit + send + manage members)
- send can view, edit, and send emails
- edit can view and modify drafts/services
- view can only read data
Permission Checking
TherequirePermission() function enforces permission requirements:
session.ts
Permission-Restricted Endpoints
Some endpoints require specific permissions:Send Email (requires or 'admin')
Manage Members (requires only)
Making Authenticated Requests
From the Browser
When making requests from the DelightBridge frontend, the session cookie is automatically included:Frontend Example
From External Clients
To make API requests from external tools (e.g., curl, Postman), you need to:- Log in via the web interface to obtain a session cookie
- Extract the session cookie from your browser’s developer tools
- Include the cookie in your requests
cURL Example
cURL with Multiple Cookies
Admin Emails
Users can be designated as admins via environment variables. These users automatically receiveadmin permissions regardless of database settings:
.env.local
Admin emails defined in environment variables cannot be downgraded or removed through the API. This provides a safeguard for permanent admin access.
/src/lib/admin-emails.ts and enforced in both authentication and permission checks:
auth.ts
Session Object Structure
The session object available in API routes contains:Session Structure
Error Responses
401 Unauthorized
Returned when no valid session exists:/login to authenticate.
403 Forbidden
Returned when the user lacks required permissions:view permission trying to send an email.
Workspace Members
Workspace access is controlled through theworkspace_members table. Only users whose email addresses are in this table (or in ADMIN_EMAILS) can log in:
Sign-In Check
Security Best Practices
Use HTTPS in production
Use HTTPS in production
Always deploy DelightBridge with HTTPS enabled. NextAuth automatically uses secure cookies (
__Secure-* prefix) when served over HTTPS.Rotate session secrets
Rotate session secrets
Set a strong Generate a new secret with:
AUTH_SECRET in your environment variables:Session expiration
Session expiration
NextAuth sessions expire after 30 days by default. Users must re-authenticate after this period.
Audit permission changes
Audit permission changes
Track permission changes in your workspace members. Consider logging who modifies permissions and when.
Next Steps
Services API
Learn how to manage email service accounts
Threads API
Fetch and manage email threads
Members API
Manage workspace members (admin only)
Permissions Guide
Detailed guide on permission management