Onboarding

This commit is contained in:
2026-05-08 13:00:30 +02:00
parent d37b49f1f6
commit 9386ae1be7
37 changed files with 5606 additions and 2275 deletions

View File

@@ -291,4 +291,55 @@ const signInWithApple = async (db, identityToken, profile = {}) => {
return { id, email: normalizedEmail, name, isNewUser: true };
};
module.exports = { ensureAuthSchema, signUp, login, signInWithApple, issueToken, verifyJwt, verifyAppleIdentityToken };
const runInTransaction = async (db, worker) => {
const client = typeof db.connect === 'function' ? await db.connect() : db;
const release = typeof client.release === 'function' ? () => client.release() : () => {};
await run(client, 'BEGIN');
try {
const result = await worker(client);
await run(client, 'COMMIT');
return result;
} catch (error) {
try {
await run(client, 'ROLLBACK');
} catch (rollbackError) {
console.error('Failed to rollback account deletion transaction.', rollbackError);
}
throw error;
} finally {
release();
}
};
const deleteAccount = async (db, userId) => {
if (!userId || typeof userId !== 'string') {
const err = new Error('Valid user id is required.');
err.code = 'BAD_REQUEST';
err.status = 400;
throw err;
}
return runInTransaction(db, async (tx) => {
await run(tx, 'DELETE FROM billing_accounts WHERE user_id = $1', [userId]);
await run(
tx,
`DELETE FROM billing_idempotency
WHERE id LIKE $1 OR id LIKE $2`,
[`endpoint:%:${userId}:%`, `charge:%:${userId}:%`],
);
const result = await run(tx, 'DELETE FROM auth_users WHERE id = $1', [userId]);
return { deleted: result.changes > 0 };
});
};
module.exports = {
deleteAccount,
ensureAuthSchema,
signUp,
login,
signInWithApple,
issueToken,
verifyJwt,
verifyAppleIdentityToken,
};