Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions functions/send-email-link/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { send as sendSmtp } from 'simple-smtp-server';
import { parseEnvBoolean } from '@pgpmjs/env';
import { createLogger } from '@pgpmjs/logger';

const isDryRun = parseEnvBoolean(process.env.SEND_EMAIL_LINK_DRY_RUN) ?? false;
const useSmtp = parseEnvBoolean(process.env.EMAIL_SEND_USE_SMTP) ?? false;
const isDryRun = parseEnvBoolean(process.env.SEND_EMAIL_LINK_DRY_RUN) ?? false; // skip actual email sending, log only
const useSmtp = parseEnvBoolean(process.env.EMAIL_SEND_USE_SMTP) ?? false; // use SMTP instead of Mailgun
const allowLocalhost = parseEnvBoolean(process.env.ALLOW_LOCALHOST) ?? false; // enable http:// and custom port for localhost URLs
const localAppPort = process.env.LOCAL_APP_PORT; // port for local dev URLs (e.g., 3001)
const logger = createLogger('send-email-link');
const app = createJobApp();

Expand Down Expand Up @@ -212,15 +214,15 @@ export const sendEmailLink = async (

// Optional: LOCAL_APP_PORT lets you attach a port for local dashboards
// e.g. LOCAL_APP_PORT=3000 -> http://localhost:3000
// It is ignored for non-local hostnames. Only allow on DRY RUNs
// It is ignored for non-local hostnames. Only allow when ALLOW_LOCALHOST=true
const localPort =
isLocalHost && isDryRun && process.env.LOCAL_APP_PORT
? `:${process.env.LOCAL_APP_PORT}`
isLocalHost && allowLocalhost && localAppPort
? `:${localAppPort}`
: '';

// Use http only for local dry-run to avoid browser TLS warnings
// in dev; production stays https.
const protocol = isLocalHost && isDryRun ? 'http' : 'https';
// Use http only for local dev (when ALLOW_LOCALHOST=true) to avoid browser
// TLS warnings; production stays https.
const protocol = isLocalHost && allowLocalhost ? 'http' : 'https';
const url = new URL(`${protocol}://${hostname}${localPort}`);

let subject: string;
Expand Down Expand Up @@ -403,9 +405,10 @@ if (require.main === module) {
defaultDatabaseId: process.env.DEFAULT_DATABASE_ID || 'not set',
dryRun: isDryRun,
useSmtp,
allowLocalhost,
mailgunDomain: process.env.MAILGUN_DOMAIN || 'not set',
mailgunFrom: process.env.MAILGUN_FROM || 'not set',
localAppPort: process.env.LOCAL_APP_PORT || 'not set',
localAppPort: localAppPort || 'not set',
hasAuthToken: !!process.env.GRAPHQL_AUTH_TOKEN
});

Expand Down