summary refs log tree commit diff
path: root/api/src/routes/users/@me
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-09 13:23:36 +0200
committerGitHub <noreply@github.com>2021-10-09 13:23:36 +0200
commit89549712421b999d6cefd0a91602de9b6fdd26ff (patch)
tree73d86ef2d9b3454cca4ca92e1bd6e4481d96a21b /api/src/routes/users/@me
parent:bug: fix import (diff)
parent:art: do not auto guest register on login/register page (diff)
downloadserver-ts-89549712421b999d6cefd0a91602de9b6fdd26ff.tar.xz
Merge pull request #428 from fosscord/dev
Various fixes and guest account enhancements
Diffstat (limited to 'api/src/routes/users/@me')
-rw-r--r--api/src/routes/users/@me/index.ts34
1 files changed, 30 insertions, 4 deletions
diff --git a/api/src/routes/users/@me/index.ts b/api/src/routes/users/@me/index.ts

index f6bb04d7..1959704a 100644 --- a/api/src/routes/users/@me/index.ts +++ b/api/src/routes/users/@me/index.ts
@@ -1,6 +1,7 @@ import { Router, Request, Response } from "express"; -import { User, PrivateUserProjection, emitEvent, UserUpdateEvent, handleFile } from "@fosscord/util"; +import { User, PrivateUserProjection, emitEvent, UserUpdateEvent, handleFile, FieldErrors } from "@fosscord/util"; import { route } from "@fosscord/api"; +import bcrypt from "bcrypt"; const router: Router = Router(); @@ -32,10 +33,35 @@ 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); - await new User({ ...body, id: req.user_id }).save(); + const user = await User.findOneOrFail({ where: { id: req.user_id }, select: [...PrivateUserProjection, "data"] }); + + if (body.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); + } + } + + user.assign(body); + + if (body.new_password) { + if (!body.password && !user.email) { + throw FieldErrors({ + password: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") } + }); + } + user.data.hash = await bcrypt.hash(body.new_password, 12); + } + + await user.save(); + + // @ts-ignore + delete user.data; - //Need to reload user from db due to https://github.com/typeorm/typeorm/issues/3490 - const user = await User.findOneOrFail({ where: { id: req.user_id }, select: PrivateUserProjection }); // TODO: send update member list event in gateway await emitEvent({ event: "USER_UPDATE",