2023-05-26 21:37:12 +08:00
|
|
|
/* 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-26 21:37:12 +08:00
|
|
|
|
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
|
|
|
|
2023-06-05 04:16:49 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
2023-05-19 15:22:48 -03:00
|
|
|
const loginController: RequestHandler = async (request, response) => {
|
2023-05-26 21:37:12 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument
|
2023-05-23 20:42:06 -04:00
|
|
|
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
|
|
|
|
2023-05-23 20:42:06 -04: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
|
|
|
|
2024-05-06 15:14:09 +02:00
|
|
|
if (!account && config.autoCreateAccount && loginRequest.ClientType != "webui") {
|
2023-05-23 20:42:06 -04:00
|
|
|
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
|
2023-05-23 20:42:06 -04:00
|
|
|
});
|
2024-01-06 16:26:58 +01:00
|
|
|
logger.debug("created new account");
|
2023-06-04 03:06:22 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2023-05-23 20:42:06 -04:00
|
|
|
const { email, password, ...databaseAccount } = newAccount;
|
|
|
|
const newLoginResponse: ILoginResponse = {
|
|
|
|
...databaseAccount,
|
|
|
|
Groups: groups,
|
|
|
|
platformCDNs: platformCDNs,
|
2024-05-15 21:55:59 +02:00
|
|
|
NRS: [config.myAddress],
|
2023-05-23 20:42:06 -04:00
|
|
|
DTLS: DTLS,
|
2024-05-15 21:55:59 +02:00
|
|
|
IRC: [config.myAddress],
|
2023-05-23 20:42:06 -04:00
|
|
|
HUB: HUB,
|
2024-05-15 21:55:59 +02:00
|
|
|
BuildLabel: buildConfig.buildLabel,
|
|
|
|
MatchmakingBuildId: buildConfig.matchmakingBuildId
|
2023-05-23 20:42:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
response.json(newLoginResponse);
|
|
|
|
return;
|
|
|
|
} catch (error: unknown) {
|
|
|
|
if (error instanceof Error) {
|
2024-02-18 13:58:43 +01:00
|
|
|
throw new Error(`error creating account ${error.message}`);
|
2023-05-23 20:42:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//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;
|
|
|
|
account.save();
|
|
|
|
}
|
|
|
|
|
2023-05-23 20:42:06 -04:00
|
|
|
const { email, password, ...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,
|
2024-05-15 21:55:59 +02:00
|
|
|
IRC: [config.myAddress],
|
2023-05-19 15:22:48 -03:00
|
|
|
HUB: HUB,
|
2024-05-15 21:55:59 +02:00
|
|
|
BuildLabel: buildConfig.buildLabel,
|
|
|
|
MatchmakingBuildId: buildConfig.matchmakingBuildId
|
2023-05-23 20:42:06 -04:00
|
|
|
};
|
2023-05-19 15:22:48 -03:00
|
|
|
|
2023-05-23 20:42:06 -04:00
|
|
|
response.json(newLoginResponse);
|
2023-05-19 15:22:48 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
export { loginController };
|