Compare commits
3 Commits
cd1d7cee00
...
8d54b2c043
| Author | SHA1 | Date | |
|---|---|---|---|
| 8d54b2c043 | |||
| b23979855e | |||
| c835471f0f |
@ -8,7 +8,7 @@ import { createAccount, createNonce, getUsernameFromEmail, isCorrectPassword } f
|
||||
import type { IDatabaseAccountJson, ILoginRequest, ILoginResponse } from "../../types/loginTypes.ts";
|
||||
import { logger } from "../../utils/logger.ts";
|
||||
import { version_compare } from "../../helpers/inventoryHelpers.ts";
|
||||
import { sendWsBroadcastTo } from "../../services/wsService.ts";
|
||||
import { handleNonceInvalidation } from "../../services/wsService.ts";
|
||||
|
||||
export const loginController: RequestHandler = async (request, response) => {
|
||||
const loginRequest = JSON.parse(String(request.body)) as ILoginRequest; // parse octet stream of json data to json object
|
||||
@ -88,7 +88,7 @@ export const loginController: RequestHandler = async (request, response) => {
|
||||
account.LastLogin = new Date();
|
||||
await account.save();
|
||||
|
||||
sendWsBroadcastTo(account._id.toString(), { nonce_updated: true });
|
||||
handleNonceInvalidation(account._id.toString());
|
||||
|
||||
response.json(createLoginResponse(myAddress, myUrlBase, account.toJSON(), buildLabel));
|
||||
};
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import type { RequestHandler } from "express";
|
||||
import { Account } from "../../models/loginModel.ts";
|
||||
import { sendWsBroadcastTo } from "../../services/wsService.ts";
|
||||
import { handleNonceInvalidation } from "../../services/wsService.ts";
|
||||
|
||||
export const logoutController: RequestHandler = async (req, res) => {
|
||||
if (!req.query.accountId) {
|
||||
@ -21,7 +21,7 @@ export const logoutController: RequestHandler = async (req, res) => {
|
||||
}
|
||||
);
|
||||
if (stat.modifiedCount) {
|
||||
sendWsBroadcastTo(req.query.accountId as string, { nonce_updated: true });
|
||||
handleNonceInvalidation(req.query.accountId as string);
|
||||
}
|
||||
|
||||
res.writeHead(200, {
|
||||
|
||||
@ -6,7 +6,7 @@ import { Account } from "../models/loginModel.ts";
|
||||
import { createAccount, createNonce, getUsernameFromEmail, isCorrectPassword } from "./loginService.ts";
|
||||
import type { IDatabaseAccountJson } from "../types/loginTypes.ts";
|
||||
import type { HydratedDocument } from "mongoose";
|
||||
import { logError } from "../utils/logger.ts";
|
||||
import { logError, logger } from "../utils/logger.ts";
|
||||
|
||||
let wsServer: WebSocketServer | undefined;
|
||||
let wssServer: WebSocketServer | undefined;
|
||||
@ -158,6 +158,7 @@ const wsOnConnect = (ws: ws, req: http.IncomingMessage): void => {
|
||||
});
|
||||
if (account) {
|
||||
(ws as IWsCustomData).accountId = account.id;
|
||||
logger.debug(`got bootstrapper connection for ${account.id}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -178,16 +179,15 @@ const wsOnConnect = (ws: ws, req: http.IncomingMessage): void => {
|
||||
logError(e as Error, `processing websocket message`);
|
||||
}
|
||||
});
|
||||
ws.on("close", () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||
ws.on("close", async () => {
|
||||
if ((ws as IWsCustomData).isGame && (ws as IWsCustomData).accountId) {
|
||||
void Account.updateOne(
|
||||
{
|
||||
_id: (ws as IWsCustomData).accountId
|
||||
},
|
||||
{
|
||||
Dropped: true
|
||||
}
|
||||
).then(() => {});
|
||||
logger.debug(`lost bootstrapper connection for ${(ws as IWsCustomData).accountId}`);
|
||||
const account = await Account.findOne({ _id: (ws as IWsCustomData).accountId });
|
||||
if (account?.Nonce) {
|
||||
account.Dropped = true;
|
||||
await account.save();
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
@ -247,3 +247,28 @@ export const sendWsBroadcastEx = (data: IWsMsgToClient, accountId?: string, excl
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const handleNonceInvalidation = (accountId: string): void => {
|
||||
if (wsServer) {
|
||||
for (const client of wsServer.clients) {
|
||||
if ((client as IWsCustomData).accountId == accountId) {
|
||||
if ((client as IWsCustomData).isGame) {
|
||||
client.close();
|
||||
} else {
|
||||
client.send(JSON.stringify({ nonce_updated: true } satisfies IWsMsgToClient));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (wssServer) {
|
||||
for (const client of wssServer.clients) {
|
||||
if ((client as IWsCustomData).accountId == accountId) {
|
||||
if ((client as IWsCustomData).isGame) {
|
||||
client.close();
|
||||
} else {
|
||||
client.send(JSON.stringify({ nonce_updated: true } satisfies IWsMsgToClient));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user