SpaceNinjaServer/src/controllers/api/loginController.ts

96 lines
3.7 KiB
TypeScript
Raw Normal View History

/* eslint-disable @typescript-eslint/no-unused-vars */
import { RequestHandler } from "express";
2024-05-15 21:55:59 +02:00
import { config } from "@/src/services/configService";
import buildConfig from "@/static/data/buildConfig.json";
2023-05-19 15:22:48 -03:00
import { toLoginRequest } from "@/src/helpers/loginHelpers";
import { Account } from "@/src/models/loginModel";
import { createAccount, isCorrectPassword } from "@/src/services/loginService";
import { ILoginResponse } from "@/src/types/loginTypes";
2024-05-28 13:45:06 +02:00
import { DTLS, groups, HUB, platformCDNs } from "@/static/fixed_responses/login_static";
2024-01-06 16:26:58 +01:00
import { logger } from "@/src/utils/logger";
2023-05-19 15:22:48 -03:00
const loginController: RequestHandler = async (request, response) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument
const body = JSON.parse(request.body); // parse octet stream of json data to json object
const loginRequest = toLoginRequest(body);
2023-05-19 15:22:48 -03:00
const account = await Account.findOne({ email: loginRequest.email }); //{ _id: 0, __v: 0 }
2024-05-28 13:45:06 +02:00
const nonce = Math.round(Math.random() * Number.MAX_SAFE_INTEGER);
2023-05-19 15:22:48 -03:00
const buildLabel: string =
typeof request.query.buildLabel == "string"
? request.query.buildLabel.split(" ").join("+")
: buildConfig.buildLabel;
if (!account && config.autoCreateAccount && loginRequest.ClientType != "webui") {
try {
const newAccount = await createAccount({
email: loginRequest.email,
password: loginRequest.password,
DisplayName: loginRequest.email.substring(0, loginRequest.email.indexOf("@")),
CountryCode: loginRequest.lang.toUpperCase(),
ClientType: loginRequest.ClientType,
CrossPlatformAllowed: true,
ForceLogoutVersion: 0,
ConsentNeeded: false,
2024-05-28 13:45:06 +02:00
TrackedSettings: [],
Nonce: nonce
});
2024-01-06 16:26:58 +01:00
logger.debug("created new account");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { email, password, LastLoginDay, ...databaseAccount } = newAccount;
const newLoginResponse: ILoginResponse = {
...databaseAccount,
Groups: groups,
platformCDNs: platformCDNs,
2024-05-15 21:55:59 +02:00
NRS: [config.myAddress],
DTLS: DTLS,
IRC: config.myIrcAddresses ?? [config.myAddress],
HUB: HUB,
BuildLabel: buildLabel,
2024-05-15 21:55:59 +02:00
MatchmakingBuildId: buildConfig.matchmakingBuildId
};
response.json(newLoginResponse);
return;
} catch (error: unknown) {
if (error instanceof Error) {
throw new Error(`error creating account ${error.message}`);
}
}
}
//email not found or incorrect password
if (!account || !isCorrectPassword(loginRequest.password, account.password)) {
response.status(400).json({ error: "incorrect login data" });
return;
}
2024-05-28 13:45:06 +02:00
if (account.Nonce == 0 || loginRequest.ClientType != "webui") {
account.Nonce = nonce;
}
if (loginRequest.ClientType != "webui") {
account.CountryCode = loginRequest.lang.toUpperCase();
}
await account.save();
2024-05-28 13:45:06 +02:00
const { email, password, LastLoginDay, ...databaseAccount } = account.toJSON();
const newLoginResponse: ILoginResponse = {
2023-05-19 15:22:48 -03:00
...databaseAccount,
Groups: groups,
platformCDNs: platformCDNs,
2024-05-15 21:55:59 +02:00
NRS: [config.myAddress],
2023-05-19 15:22:48 -03:00
DTLS: DTLS,
IRC: config.myIrcAddresses ?? [config.myAddress],
2023-05-19 15:22:48 -03:00
HUB: HUB,
BuildLabel: buildLabel,
2024-05-15 21:55:59 +02:00
MatchmakingBuildId: buildConfig.matchmakingBuildId
};
2023-05-19 15:22:48 -03:00
response.json(newLoginResponse);
2023-05-19 15:22:48 -03:00
};
export { loginController };