From 57f6f37e856a6a44bf11337005350692177746bd Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Thu, 27 Mar 2025 02:05:34 +0100 Subject: [PATCH 1/2] chore: simplify logoutController Reducing 3-4 MongoDB operations to only 1. --- src/controllers/api/logoutController.ts | 33 ++++++++++++++----------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/controllers/api/logoutController.ts b/src/controllers/api/logoutController.ts index f6f8863f..92a7b59d 100644 --- a/src/controllers/api/logoutController.ts +++ b/src/controllers/api/logoutController.ts @@ -1,19 +1,24 @@ import { RequestHandler } from "express"; -import { getAccountIdForRequest } from "@/src/services/loginService"; import { Account } from "@/src/models/loginModel"; -const logoutController: RequestHandler = async (req, res) => { - const accountId = await getAccountIdForRequest(req); - const account = await Account.findById(accountId); - if (account) { - account.Nonce = 0; - await account.save(); +export const logoutController: RequestHandler = async (req, res) => { + if (!req.query.accountId) { + throw new Error("Request is missing accountId parameter"); + } + const nonce: number = parseInt(req.query.nonce as string); + if (!nonce) { + throw new Error("Request is missing nonce parameter"); } - res.writeHead(200, { - "Content-Type": "text/html", - "Content-Length": 1 - }); - res.end("1"); -}; -export { logoutController }; + await Account.updateOne( + { + _id: req.query.accountId, + Nonce: nonce + }, + { + Nonce: 0 + } + ); + + res.end(); +}; -- 2.47.2 From b2423f5d418b5d332f7c51f0c8b0c48cc48d9af7 Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Thu, 27 Mar 2025 02:07:46 +0100 Subject: [PATCH 2/2] keep response as-is --- src/controllers/api/logoutController.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/controllers/api/logoutController.ts b/src/controllers/api/logoutController.ts index 92a7b59d..889e7d78 100644 --- a/src/controllers/api/logoutController.ts +++ b/src/controllers/api/logoutController.ts @@ -20,5 +20,9 @@ export const logoutController: RequestHandler = async (req, res) => { } ); - res.end(); + res.writeHead(200, { + "Content-Type": "text/html", + "Content-Length": 1 + }); + res.end("1"); }; -- 2.47.2