SMTP: - Add connection/greeting/socket timeouts so a stalled Gmail connection no longer hangs sign-up - Wrap sendMail in try/catch and fall back to logging the code - Derive secure from port (465 implicit TLS vs 587 STARTTLS) - Strip whitespace from the Gmail app password - Document SMTP_HOST/SMTP_PORT in .env.example and environment.d.ts Password reset (new): - POST /(api)/auth/forgot-password emails a 6-digit code and does not reveal whether the address is registered - POST /(api)/auth/reset-password validates the code, sets the new password, verifies the email, and signs the user in - password_reset_codes table added to seed-db.mjs - "Forgot password?" flow on the mobile sign-in screen User deletion (new): - DELETE /(api)/admin/users/[id], owner-only, blocks self-deletion - Delete button with confirmation on the dashboard Users page Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
76 lines
1.7 KiB
TypeScript
76 lines
1.7 KiB
TypeScript
import { Pool, type QueryResultRow } from "pg";
|
|
|
|
const pool = new Pool({
|
|
connectionString: process.env.DATABASE_URL,
|
|
max: 10,
|
|
idleTimeoutMillis: 30_000,
|
|
connectionTimeoutMillis: 10_000,
|
|
});
|
|
|
|
export type SqlValue = string | number | boolean | null | Date;
|
|
|
|
export async function sql<R extends QueryResultRow = QueryResultRow>(
|
|
strings: TemplateStringsArray,
|
|
...values: SqlValue[]
|
|
): Promise<R[]> {
|
|
const text = strings.reduce(
|
|
(acc, chunk, i) =>
|
|
acc + chunk + (i < values.length ? `$${i + 1}` : ""),
|
|
"",
|
|
);
|
|
|
|
const result = await pool.query<R>(text, values);
|
|
|
|
return result.rows;
|
|
}
|
|
|
|
export async function query<R extends QueryResultRow = QueryResultRow>(
|
|
text: string,
|
|
values: SqlValue[] = [],
|
|
): Promise<R[]> {
|
|
const result = await pool.query<R>(text, values);
|
|
return result.rows;
|
|
}
|
|
|
|
export async function transaction<T>(
|
|
callback: (
|
|
tx: <R extends QueryResultRow = QueryResultRow>(
|
|
strings: TemplateStringsArray,
|
|
...values: SqlValue[]
|
|
) => Promise<R[]>,
|
|
) => Promise<T>,
|
|
): Promise<T> {
|
|
const client = await pool.connect();
|
|
|
|
try {
|
|
await client.query("BEGIN");
|
|
|
|
const tx = async <R extends QueryResultRow = QueryResultRow>(
|
|
strings: TemplateStringsArray,
|
|
...values: SqlValue[]
|
|
) => {
|
|
const text = strings.reduce(
|
|
(acc, chunk, i) =>
|
|
acc + chunk + (i < values.length ? `$${i + 1}` : ""),
|
|
"",
|
|
);
|
|
|
|
const result = await client.query<R>(text, values);
|
|
|
|
return result.rows;
|
|
};
|
|
|
|
const out = await callback(tx);
|
|
|
|
await client.query("COMMIT");
|
|
|
|
return out;
|
|
} catch (error) {
|
|
await client.query("ROLLBACK");
|
|
|
|
throw error;
|
|
} finally {
|
|
client.release();
|
|
}
|
|
}
|