Merge pull request #381 from TNG-dev:require-invite-code-json5

Add support for ENABLE_PUBLIC_SIGNUP
This commit is contained in:
zkldi
2021-11-01 01:27:03 +00:00
committed by GitHub
2 changed files with 30 additions and 25 deletions
+2
View File
@@ -74,6 +74,7 @@ export interface TachiConfig {
OUR_URL: string;
LOGGER_DISCORD_WEBHOOK?: string;
DISCORD_WHO_TO_TAG?: string[];
ENABLE_PUBLIC_SIGNUP?: boolean;
}
const isValidOauth2 = p.optional({
@@ -112,6 +113,7 @@ const err = p(config, {
OUR_URL: "string",
LOGGER_DISCORD_WEBHOOK: "*string",
DISCORD_WHO_TO_TAG: p.optional(["string"]),
ENABLE_PUBLIC_SIGNUP: "*boolean",
});
if (err) {
+28 -25
View File
@@ -161,7 +161,7 @@ router.post(
username: Prudence.regex(/^[a-zA-Z_-][a-zA-Z0-9_-]{2,20}$/u),
password: ValidatePassword,
email: Prudence.regex(LAZY_EMAIL_REGEX),
inviteCode: "string",
inviteCode: "*string",
captcha: "string",
},
{
@@ -218,30 +218,33 @@ router.post(
try {
const userID = await GetNextCounterValue("users");
const inviteCodeDoc = await db.invites.findOneAndUpdate(
{
code: req.body.inviteCode,
consumed: false,
},
{
$set: {
consumed: true,
consumedAt: Date.now(),
consumedBy: userID,
if (!ServerConfig.ENABLE_PUBLIC_SIGNUP) {
const inviteCodeDoc = await db.invites.findOneAndUpdate(
{
code: req.body.inviteCode,
consumed: false,
},
{
$set: {
consumed: true,
consumedAt: Date.now(),
consumedBy: userID,
},
}
);
if (!inviteCodeDoc) {
logger.info(`Invalid invite code given: ${req.body.inviteCode}.`);
return res.status(401).json({
success: false,
description: `This invite code is not valid.`,
});
}
);
if (!inviteCodeDoc) {
logger.info(`Invalid invite code given: ${req.body.inviteCode}.`);
return res.status(401).json({
success: false,
description: `This invite code is not valid.`,
});
logger.info(`Consumed invite ${inviteCodeDoc.code}.`);
}
logger.info(`Consumed invite ${inviteCodeDoc.code}.`);
// if we get to this point, We're good to create the user.
const { newUser, newSettings } = await AddNewUser(
@@ -276,12 +279,12 @@ router.post(
body: user,
});
} catch (err) {
logger.error(
`Bailed on user creation ${req.body.username} with invite code ${req.body.inviteCode}.`,
{ err }
);
logger.error(`Bailed on user creation ${req.body.username}.`, { err });
if (!ServerConfig.ENABLE_PUBLIC_SIGNUP) {
await ReinstateInvite(req.body.inviteCode);
}
await ReinstateInvite(req.body.inviteCode);
await DecrementCounterValue("users");
return res.status(500).json({