summary refs log tree commit diff
path: root/src/api/routes/auth/register.ts
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-09-26 22:29:30 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-09-26 22:41:21 +1000
commit99ee7e9400f06e8718612d8b52d15215dc620774 (patch)
tree08de8c5d3985b9c2eaa419f5198f891ecd82d012 /src/api/routes/auth/register.ts
parentRemove the cdn storage location log (diff)
downloadserver-99ee7e9400f06e8718612d8b52d15215dc620774.tar.xz
Prettier
Diffstat (limited to 'src/api/routes/auth/register.ts')
-rw-r--r--src/api/routes/auth/register.ts303
1 files changed, 181 insertions, 122 deletions
diff --git a/src/api/routes/auth/register.ts b/src/api/routes/auth/register.ts
index 84f8f838..3479c4a0 100644
--- a/src/api/routes/auth/register.ts
+++ b/src/api/routes/auth/register.ts
@@ -1,156 +1,215 @@
 import { Request, Response, Router } from "express";
-import { Config, generateToken, Invite, FieldErrors, User, adjustEmail, RegisterSchema } from "@fosscord/util";
-import { route, getIpAdress, IPAnalysis, isProxy, verifyCaptcha } from "@fosscord/api";
+import {
+	Config,
+	generateToken,
+	Invite,
+	FieldErrors,
+	User,
+	adjustEmail,
+	RegisterSchema,
+} from "@fosscord/util";
+import {
+	route,
+	getIpAdress,
+	IPAnalysis,
+	isProxy,
+	verifyCaptcha,
+} from "@fosscord/api";
 import bcrypt from "bcrypt";
 import { HTTPError } from "lambert-server";
 
 const router: Router = Router();
 
-router.post("/", route({ body: "RegisterSchema" }), async (req: Request, res: Response) => {
-	const body = req.body as RegisterSchema;
-	const { register, security } = Config.get();
-	const ip = getIpAdress(req);
-
-	// email will be slightly modified version of the user supplied email -> e.g. protection against GMail Trick
-	let email = adjustEmail(body.email);
-
-	// check if registration is allowed
-	if (!register.allowNewRegistration) {
-		throw FieldErrors({
-			email: { code: "REGISTRATION_DISABLED", message: req.t("auth:register.REGISTRATION_DISABLED") }
-		});
-	}
-
-	// check if the user agreed to the Terms of Service
-	if (!body.consent) {
-		throw FieldErrors({
-			consent: { code: "CONSENT_REQUIRED", message: req.t("auth:register.CONSENT_REQUIRED") }
-		});
-	}
-
-	if (register.disabled) {
-		throw FieldErrors({
-			email: {
-				code: "DISABLED",
-				message: "registration is disabled on this instance"
-			}
-		});
-	}
-
-	if (register.requireCaptcha && security.captcha.enabled) {
-		const { sitekey, service } = security.captcha;
-		if (!body.captcha_key) {
-			return res?.status(400).json({
-				captcha_key: ["captcha-required"],
-				captcha_sitekey: sitekey,
-				captcha_service: service
+router.post(
+	"/",
+	route({ body: "RegisterSchema" }),
+	async (req: Request, res: Response) => {
+		const body = req.body as RegisterSchema;
+		const { register, security } = Config.get();
+		const ip = getIpAdress(req);
+
+		// email will be slightly modified version of the user supplied email -> e.g. protection against GMail Trick
+		let email = adjustEmail(body.email);
+
+		// check if registration is allowed
+		if (!register.allowNewRegistration) {
+			throw FieldErrors({
+				email: {
+					code: "REGISTRATION_DISABLED",
+					message: req.t("auth:register.REGISTRATION_DISABLED"),
+				},
 			});
 		}
 
-		const verify = await verifyCaptcha(body.captcha_key, ip);
-		if (!verify.success) {
-			return res.status(400).json({
-				captcha_key: verify["error-codes"],
-				captcha_sitekey: sitekey,
-				captcha_service: service
+		// check if the user agreed to the Terms of Service
+		if (!body.consent) {
+			throw FieldErrors({
+				consent: {
+					code: "CONSENT_REQUIRED",
+					message: req.t("auth:register.CONSENT_REQUIRED"),
+				},
 			});
 		}
-	}
-
-	if (!register.allowMultipleAccounts) {
-		// TODO: check if fingerprint was eligible generated
-		const exists = await User.findOne({ where: { fingerprints: body.fingerprint }, select: ["id"] });
 
-		if (exists) {
+		if (register.disabled) {
 			throw FieldErrors({
 				email: {
-					code: "EMAIL_ALREADY_REGISTERED",
-					message: req.t("auth:register.EMAIL_ALREADY_REGISTERED")
-				}
+					code: "DISABLED",
+					message: "registration is disabled on this instance",
+				},
 			});
 		}
-	}
 
-	if (register.blockProxies) {
-		if (isProxy(await IPAnalysis(ip))) {
-			console.log(`proxy ${ip} blocked from registration`);
-			throw new HTTPError("Your IP is blocked from registration");
+		if (register.requireCaptcha && security.captcha.enabled) {
+			const { sitekey, service } = security.captcha;
+			if (!body.captcha_key) {
+				return res?.status(400).json({
+					captcha_key: ["captcha-required"],
+					captcha_sitekey: sitekey,
+					captcha_service: service,
+				});
+			}
+
+			const verify = await verifyCaptcha(body.captcha_key, ip);
+			if (!verify.success) {
+				return res.status(400).json({
+					captcha_key: verify["error-codes"],
+					captcha_sitekey: sitekey,
+					captcha_service: service,
+				});
+			}
 		}
-	}
 
-	// TODO: gift_code_sku_id?
-	// TODO: check password strength
+		if (!register.allowMultipleAccounts) {
+			// TODO: check if fingerprint was eligible generated
+			const exists = await User.findOne({
+				where: { fingerprints: body.fingerprint },
+				select: ["id"],
+			});
+
+			if (exists) {
+				throw FieldErrors({
+					email: {
+						code: "EMAIL_ALREADY_REGISTERED",
+						message: req.t(
+							"auth:register.EMAIL_ALREADY_REGISTERED",
+						),
+					},
+				});
+			}
+		}
 
-	if (email) {
-		// replace all dots and chars after +, if its a gmail.com email
-		if (!email) {
-			throw FieldErrors({ email: { code: "INVALID_EMAIL", message: req?.t("auth:register.INVALID_EMAIL") } });
+		if (register.blockProxies) {
+			if (isProxy(await IPAnalysis(ip))) {
+				console.log(`proxy ${ip} blocked from registration`);
+				throw new HTTPError("Your IP is blocked from registration");
+			}
 		}
 
-		// check if there is already an account with this email
-		const exists = await User.findOne({ where: { email: email } });
+		// TODO: gift_code_sku_id?
+		// TODO: check password strength
+
+		if (email) {
+			// replace all dots and chars after +, if its a gmail.com email
+			if (!email) {
+				throw FieldErrors({
+					email: {
+						code: "INVALID_EMAIL",
+						message: req?.t("auth:register.INVALID_EMAIL"),
+					},
+				});
+			}
 
-		if (exists) {
+			// check if there is already an account with this email
+			const exists = await User.findOne({ where: { email: email } });
+
+			if (exists) {
+				throw FieldErrors({
+					email: {
+						code: "EMAIL_ALREADY_REGISTERED",
+						message: req.t(
+							"auth:register.EMAIL_ALREADY_REGISTERED",
+						),
+					},
+				});
+			}
+		} else if (register.email.required) {
 			throw FieldErrors({
 				email: {
-					code: "EMAIL_ALREADY_REGISTERED",
-					message: req.t("auth:register.EMAIL_ALREADY_REGISTERED")
-				}
+					code: "BASE_TYPE_REQUIRED",
+					message: req.t("common:field.BASE_TYPE_REQUIRED"),
+				},
 			});
 		}
-	} else if (register.email.required) {
-		throw FieldErrors({
-			email: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") }
-		});
-	}
-
-	if (register.dateOfBirth.required && !body.date_of_birth) {
-		throw FieldErrors({
-			date_of_birth: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") }
-		});
-	} else if (register.dateOfBirth.required && register.dateOfBirth.minimum) {
-		const minimum = new Date();
-		minimum.setFullYear(minimum.getFullYear() - register.dateOfBirth.minimum);
-		body.date_of_birth = new Date(body.date_of_birth as Date);
-
-		// higher is younger
-		if (body.date_of_birth > minimum) {
+
+		if (register.dateOfBirth.required && !body.date_of_birth) {
 			throw FieldErrors({
 				date_of_birth: {
-					code: "DATE_OF_BIRTH_UNDERAGE",
-					message: req.t("auth:register.DATE_OF_BIRTH_UNDERAGE", { years: register.dateOfBirth.minimum })
-				}
+					code: "BASE_TYPE_REQUIRED",
+					message: req.t("common:field.BASE_TYPE_REQUIRED"),
+				},
+			});
+		} else if (
+			register.dateOfBirth.required &&
+			register.dateOfBirth.minimum
+		) {
+			const minimum = new Date();
+			minimum.setFullYear(
+				minimum.getFullYear() - register.dateOfBirth.minimum,
+			);
+			body.date_of_birth = new Date(body.date_of_birth as Date);
+
+			// higher is younger
+			if (body.date_of_birth > minimum) {
+				throw FieldErrors({
+					date_of_birth: {
+						code: "DATE_OF_BIRTH_UNDERAGE",
+						message: req.t("auth:register.DATE_OF_BIRTH_UNDERAGE", {
+							years: register.dateOfBirth.minimum,
+						}),
+					},
+				});
+			}
+		}
+
+		if (body.password) {
+			// the salt is saved in the password refer to bcrypt docs
+			body.password = await bcrypt.hash(body.password, 12);
+		} else if (register.password.required) {
+			throw FieldErrors({
+				password: {
+					code: "BASE_TYPE_REQUIRED",
+					message: req.t("common:field.BASE_TYPE_REQUIRED"),
+				},
+			});
+		}
+
+		if (
+			!body.invite &&
+			(register.requireInvite ||
+				(register.guestsRequireInvite && !register.email))
+		) {
+			// require invite to register -> e.g. for organizations to send invites to their employees
+			throw FieldErrors({
+				email: {
+					code: "INVITE_ONLY",
+					message: req.t("auth:register.INVITE_ONLY"),
+				},
 			});
 		}
-	}
-
-	if (body.password) {
-		// the salt is saved in the password refer to bcrypt docs
-		body.password = await bcrypt.hash(body.password, 12);
-	} else if (register.password.required) {
-		throw FieldErrors({
-			password: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") }
-		});
-	}
-
-	if (!body.invite && (register.requireInvite || (register.guestsRequireInvite && !register.email))) {
-		// require invite to register -> e.g. for organizations to send invites to their employees
-		throw FieldErrors({
-			email: { code: "INVITE_ONLY", message: req.t("auth:register.INVITE_ONLY") }
-		});
-	}
-
-	const user = await User.register({ ...body, req });
-
-	if (body.invite) {
-		// await to fail if the invite doesn't exist (necessary for requireInvite to work properly) (username only signups are possible)
-		await Invite.joinGuild(user.id, body.invite);
-	}
-
-	console.log("register", body.email, body.username, ip);
-
-	return res.json({ token: await generateToken(user.id) });
-});
+
+		const user = await User.register({ ...body, req });
+
+		if (body.invite) {
+			// await to fail if the invite doesn't exist (necessary for requireInvite to work properly) (username only signups are possible)
+			await Invite.joinGuild(user.id, body.invite);
+		}
+
+		console.log("register", body.email, body.username, ip);
+
+		return res.json({ token: await generateToken(user.id) });
+	},
+);
 
 export default router;