summary refs log tree commit diff
path: root/api/src/routes/users/@me/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'api/src/routes/users/@me/index.ts')
-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",