From f6513420be12f1c318af38dd639af590c581774c Mon Sep 17 00:00:00 2001 From: Sainan Date: Sun, 9 Mar 2025 07:40:37 -0700 Subject: [PATCH] feat: login conflict (#1127) Closes #1076 Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/1127 --- src/controllers/api/loginController.ts | 15 ++++++++++++--- .../custom/ircDroppedController.ts | 9 +++++++++ src/models/loginModel.ts | 1 + src/routes/custom.ts | 2 ++ src/services/loginService.ts | 19 +++++-------------- src/types/loginTypes.ts | 2 ++ 6 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 src/controllers/custom/ircDroppedController.ts diff --git a/src/controllers/api/loginController.ts b/src/controllers/api/loginController.ts index e1fbbb43..0e7c53fb 100644 --- a/src/controllers/api/loginController.ts +++ b/src/controllers/api/loginController.ts @@ -61,10 +61,19 @@ export const loginController: RequestHandler = async (request, response) => { return; } - if (account.Nonce == 0 || loginRequest.ClientType != "webui") { + if (loginRequest.ClientType == "webui") { + if (!account.Nonce) { + account.ClientType = "webui"; + account.Nonce = nonce; + } + } else { + if (account.Nonce && account.ClientType != "webui" && !account.Dropped && !loginRequest.kick) { + response.status(400).json({ error: "nonce still set" }); + return; + } + + account.ClientType = loginRequest.ClientType; account.Nonce = nonce; - } - if (loginRequest.ClientType != "webui") { account.CountryCode = loginRequest.lang.toUpperCase(); } await account.save(); diff --git a/src/controllers/custom/ircDroppedController.ts b/src/controllers/custom/ircDroppedController.ts new file mode 100644 index 00000000..8927c5bb --- /dev/null +++ b/src/controllers/custom/ircDroppedController.ts @@ -0,0 +1,9 @@ +import { getAccountForRequest } from "@/src/services/loginService"; +import { RequestHandler } from "express"; + +export const ircDroppedController: RequestHandler = async (req, res) => { + const account = await getAccountForRequest(req); + account.Dropped = true; + await account.save(); + res.end(); +}; diff --git a/src/models/loginModel.ts b/src/models/loginModel.ts index eb3d1576..75a12356 100644 --- a/src/models/loginModel.ts +++ b/src/models/loginModel.ts @@ -20,6 +20,7 @@ const databaseAccountSchema = new Schema( ConsentNeeded: { type: Boolean, required: true }, TrackedSettings: { type: [String], default: [] }, Nonce: { type: Number, default: 0 }, + Dropped: Boolean, LastLoginDay: { type: Number }, LatestEventMessageDate: { type: Date, default: 0 } }, diff --git a/src/routes/custom.ts b/src/routes/custom.ts index 00fe18f3..7f53ad3e 100644 --- a/src/routes/custom.ts +++ b/src/routes/custom.ts @@ -7,6 +7,7 @@ import { popArchonCrystalUpgradeController } from "@/src/controllers/custom/popA import { deleteAccountController } from "@/src/controllers/custom/deleteAccountController"; import { getNameController } from "@/src/controllers/custom/getNameController"; import { renameAccountController } from "@/src/controllers/custom/renameAccountController"; +import { ircDroppedController } from "@/src/controllers/custom/ircDroppedController"; import { createAccountController } from "@/src/controllers/custom/createAccountController"; import { createMessageController } from "@/src/controllers/custom/createMessageController"; @@ -28,6 +29,7 @@ customRouter.get("/popArchonCrystalUpgrade", popArchonCrystalUpgradeController); customRouter.get("/deleteAccount", deleteAccountController); customRouter.get("/getName", getNameController); customRouter.get("/renameAccount", renameAccountController); +customRouter.get("/ircDropped", ircDroppedController); customRouter.post("/createAccount", createAccountController); customRouter.post("/createMessage", createMessageController); diff --git a/src/services/loginService.ts b/src/services/loginService.ts index 35b3feea..71236f07 100644 --- a/src/services/loginService.ts +++ b/src/services/loginService.ts @@ -82,21 +82,12 @@ export const getAccountForRequest = async (req: Request): Promise => { - if (!req.query.accountId) { - throw new Error("Request is missing accountId parameter"); + const account = await getAccountForRequest(req); + if (account.Dropped && req.query.ct) { + account.Dropped = undefined; + await account.save(); } - if (!req.query.nonce || parseInt(req.query.nonce as string) === 0) { - throw new Error("Request is missing nonce parameter"); - } - if ( - !(await Account.exists({ - _id: req.query.accountId, - Nonce: req.query.nonce - })) - ) { - throw new Error("Invalid accountId-nonce pair"); - } - return req.query.accountId as string; + return account._id.toString(); }; export const isAdministrator = (account: TAccountDocument): boolean => { diff --git a/src/types/loginTypes.ts b/src/types/loginTypes.ts index 687d611e..108b0417 100644 --- a/src/types/loginTypes.ts +++ b/src/types/loginTypes.ts @@ -14,6 +14,7 @@ export interface IAccountAndLoginResponseCommons { export interface IDatabaseAccount extends IAccountAndLoginResponseCommons { email: string; password: string; + Dropped?: boolean; LastLoginDay?: number; LatestEventMessageDate: Date; } @@ -32,6 +33,7 @@ export interface ILoginRequest { date: number; ClientType: string; PS: string; + kick?: boolean; } export interface ILoginResponse extends IAccountAndLoginResponseCommons {