SpaceNinjaServer.CN/src/controllers/api/logoutController.ts

34 lines
946 B
TypeScript
Raw Normal View History

2023-06-01 17:08:05 -07:00
import { RequestHandler } from "express";
2024-05-28 13:45:06 +02:00
import { Account } from "@/src/models/loginModel";
import { sendWsBroadcastTo } from "@/src/services/webService";
2023-06-01 17:08:05 -07:00
export const logoutController: RequestHandler = async (req, res) => {
if (!req.query.accountId) {
throw new Error("Request is missing accountId parameter");
2024-05-28 13:45:06 +02:00
}
const nonce: number = parseInt(req.query.nonce as string);
if (!nonce) {
throw new Error("Request is missing nonce parameter");
}
const stat = await Account.updateOne(
{
_id: req.query.accountId,
Nonce: nonce
},
{
Nonce: 0
}
);
if (stat.modifiedCount) {
// Tell WebUI its nonce has been invalidated
sendWsBroadcastTo(req.query.accountId as string, { logged_out: true });
}
2023-06-01 17:08:05 -07:00
res.writeHead(200, {
"Content-Type": "text/html",
"Content-Length": 1
2023-06-01 17:08:05 -07:00
});
res.end("1");
2023-06-01 17:08:05 -07:00
};