Netlify & PostgreSQL Functions
Netlify Functions & PostgreSQL Integration
Freak-Flix utilizes Netlify Functions as a serverless backend to handle user authentication, profile persistence, and API proxying. These functions interact with a PostgreSQL database to ensure your library settings and user data are synchronized across devices.
⚙️ Database Configuration
To enable the backend features, you must provide a PostgreSQL connection string. The functions automatically look for the following environment variables in your Netlify dashboard:
| Variable | Description |
| :--- | :--- |
| DATABASE_URL | Primary PostgreSQL connection string (e.g., postgres://user:pass@host:port/db). |
| JWT_SECRET | A secure string used to sign and verify application-level JSON Web Tokens. |
| JWT_EXPIRY | (Optional) Token duration, defaults to 7d. |
🔐 Authentication Services
The backend supports two authentication flows: Independent App Auth (built-in) and Netlify Identity Integration.
1. Independent User Management (/api/users)
This service handles direct registration and login within the application database.
Endpoints:
POST /api/users?action=register: Create a new account.POST /api/users?action=login: Authenticate and receive a JWT.POST /api/users?action=me: Verify a token and return the current user.
Request Body (JSON):
{
"email": "user@example.com",
"password": "your-password",
"action": "login"
}
2. Netlify Identity Sync (/api/identity-webhook)
If using Netlify's built-in Identity service, this function acts as a webhook to automatically synchronize Identity users into your PostgreSQL identity_users table upon signup or login.
3. Profile Retrieval (/api/get-profile)
Retrieves extended user metadata for users authenticated via Netlify Identity.
- Authorization: Requires a valid Netlify Identity JWT in the
Authorization: Bearer <token>header.
🌐 Proxy Services
To bypass Cross-Origin Resource Sharing (CORS) restrictions in web builds, Freak-Flix includes specialized proxy functions.
Microsoft Authentication Proxy (/api/ms_auth/*)
Proxies requests to login.microsoftonline.com for OneDrive integration.
- Usage: Replace the base Microsoft login URL with your Netlify function URL.
- Example Path:
/.netlify/functions/ms_auth_proxy/common/oauth2/v2.0/token - Supported Methods:
GET,POST,OPTIONS.
StashDB Proxy (/api/stash_proxy)
Proxies GraphQL queries to StashDB to protect API keys and handle CORS.
- Endpoint:
POST /api/stash_proxy - Headers Required:
ApiKey: Your StashDB API Key.Content-Type:application/json
- Request Body: Standard StashDB GraphQL query payload.
🛠 Internal Data Structures
While managed automatically, the following tables are initialized in your PostgreSQL database:
app_users: Stores email and hashed passwords for the independent auth system.identity_users: Stores synchronized profiles from Netlify Identity, includingdisplay_nameand custommetadataJSON blobs.
[!TIP] When deploying to Netlify, ensure your
netlify.tomlis configured to route/api/*to/.netlify/functions/*for the cleanest API interface.