Onboarding
This commit is contained in:
@@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user