summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-09 13:04:27 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-09 13:04:27 +0200
commit976a8e094b5618f9e237dedeeb48864510773343 (patch)
tree680f8da8e700d342457d79b836fbb599abf3fdad
parent:sparkles: added guestsRequireInvite to config (diff)
downloadserver-976a8e094b5618f9e237dedeeb48864510773343.tar.xz
:bug: fix claim account
-rw-r--r--api/src/routes/users/@me/index.ts16
1 files changed, 12 insertions, 4 deletions
diff --git a/api/src/routes/users/@me/index.ts b/api/src/routes/users/@me/index.ts
index c5f490d3..1959704a 100644
--- a/api/src/routes/users/@me/index.ts
+++ b/api/src/routes/users/@me/index.ts
@@ -33,12 +33,16 @@ router.patch("/", route({ body: "UserModifySchema" }), async (req: Request, res:
 	if (body.avatar) body.avatar = await handleFile(`/avatars/${req.user_id}`, body.avatar as string);
 	if (body.banner) body.banner = await handleFile(`/banners/${req.user_id}`, body.banner as string);
 
-	const user = await User.findOneOrFail({ where: { id: req.user_id }, select: PrivateUserProjection });
+	const user = await User.findOneOrFail({ where: { id: req.user_id }, select: [...PrivateUserProjection, "data"] });
 
 	if (body.password) {
-		const same_password = await bcrypt.compare(body.password, user.data.hash || "");
-		if (!same_password) {
-			throw FieldErrors({ password: { message: req.t("auth:login.INVALID_PASSWORD"), code: "INVALID_PASSWORD" } });
+		if (user.data?.hash) {
+			const same_password = await bcrypt.compare(body.password, user.data.hash || "");
+			if (!same_password) {
+				throw FieldErrors({ password: { message: req.t("auth:login.INVALID_PASSWORD"), code: "INVALID_PASSWORD" } });
+			}
+		} else {
+			user.data.hash = await bcrypt.hash(body.password, 12);
 		}
 	}
 
@@ -54,6 +58,10 @@ router.patch("/", route({ body: "UserModifySchema" }), async (req: Request, res:
 	}
 
 	await user.save();
+
+	// @ts-ignore
+	delete user.data;
+
 	// TODO: send update member list event in gateway
 	await emitEvent({
 		event: "USER_UPDATE",