Add handling incase userdoc is created but registering still fails.

This commit is contained in:
zkldi
2021-11-02 18:51:13 +00:00
parent 0f31c6ad80
commit 3765a95c84
@@ -26,6 +26,7 @@ import { SendEmail } from "lib/email/client";
import { EmailFormatResetPassword, EmailFormatVerifyEmail } from "lib/email/formats";
import { Random20Hex } from "utils/misc";
import { ServerConfig } from "lib/setup/config";
import { integer } from ".pnpm/tachi-common@0.2.36/node_modules/tachi-common";
const logger = CreateLogCtx(__filename);
@@ -216,6 +217,8 @@ router.post(
});
}
let hasInsertedUserID: integer | null = null;
try {
const userID = await GetNextCounterValue("users");
@@ -258,6 +261,8 @@ router.post(
throw new Error("AddNewUser failed to create a user.");
}
hasInsertedUserID = newUser.id;
// re-fetch the user like this so we guaranteeably omit the private fields.
const user = await GetUserWithID(newUser.id);
@@ -285,6 +290,15 @@ router.post(
await ReinstateInvite(req.body.inviteCode);
}
if (hasInsertedUserID !== null) {
logger.warn(
`Removing user ${req.body.username} (#${hasInsertedUserID}), as their document was created, but creation still failed.`
);
await db.users.remove({ username: req.body.username });
await db["user-settings"].remove({ userID: hasInsertedUserID });
await db["user-private-information"].remove({ userID: hasInsertedUserID });
}
await DecrementCounterValue("users");
return res.status(500).json({