chore: ensure that every account has a unique DisplayName
This commit is contained in:
parent
d50c6b8c76
commit
e3bac09e21
@ -6,7 +6,7 @@ import buildConfig from "@/static/data/buildConfig.json";
|
|||||||
|
|
||||||
import { toLoginRequest } from "@/src/helpers/loginHelpers";
|
import { toLoginRequest } from "@/src/helpers/loginHelpers";
|
||||||
import { Account } from "@/src/models/loginModel";
|
import { Account } from "@/src/models/loginModel";
|
||||||
import { createAccount, isCorrectPassword } from "@/src/services/loginService";
|
import { createAccount, isCorrectPassword, isNameTaken } from "@/src/services/loginService";
|
||||||
import { IDatabaseAccountJson, ILoginResponse } from "@/src/types/loginTypes";
|
import { IDatabaseAccountJson, ILoginResponse } from "@/src/types/loginTypes";
|
||||||
import { DTLS, groups, HUB, platformCDNs } from "@/static/fixed_responses/login_static";
|
import { DTLS, groups, HUB, platformCDNs } from "@/static/fixed_responses/login_static";
|
||||||
import { logger } from "@/src/utils/logger";
|
import { logger } from "@/src/utils/logger";
|
||||||
@ -26,10 +26,19 @@ export const loginController: RequestHandler = async (request, response) => {
|
|||||||
|
|
||||||
if (!account && config.autoCreateAccount && loginRequest.ClientType != "webui") {
|
if (!account && config.autoCreateAccount && loginRequest.ClientType != "webui") {
|
||||||
try {
|
try {
|
||||||
|
const nameFromEmail = loginRequest.email.substring(0, loginRequest.email.indexOf("@"));
|
||||||
|
let name = nameFromEmail;
|
||||||
|
if (await isNameTaken(name)) {
|
||||||
|
let suffix = 0;
|
||||||
|
do {
|
||||||
|
++suffix;
|
||||||
|
name = nameFromEmail + suffix;
|
||||||
|
} while (await isNameTaken(name));
|
||||||
|
}
|
||||||
const newAccount = await createAccount({
|
const newAccount = await createAccount({
|
||||||
email: loginRequest.email,
|
email: loginRequest.email,
|
||||||
password: loginRequest.password,
|
password: loginRequest.password,
|
||||||
DisplayName: loginRequest.email.substring(0, loginRequest.email.indexOf("@")),
|
DisplayName: name,
|
||||||
CountryCode: loginRequest.lang.toUpperCase(),
|
CountryCode: loginRequest.lang.toUpperCase(),
|
||||||
ClientType: loginRequest.ClientType,
|
ClientType: loginRequest.ClientType,
|
||||||
CrossPlatformAllowed: true,
|
CrossPlatformAllowed: true,
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
import { toCreateAccount, toDatabaseAccount } from "@/src/helpers/customHelpers/customHelpers";
|
import { toCreateAccount, toDatabaseAccount } from "@/src/helpers/customHelpers/customHelpers";
|
||||||
import { createAccount } from "@/src/services/loginService";
|
import { createAccount, isNameTaken } from "@/src/services/loginService";
|
||||||
import { RequestHandler } from "express";
|
import { RequestHandler } from "express";
|
||||||
|
|
||||||
const createAccountController: RequestHandler = async (req, res) => {
|
const createAccountController: RequestHandler = async (req, res) => {
|
||||||
const createAccountData = toCreateAccount(req.body);
|
const createAccountData = toCreateAccount(req.body);
|
||||||
|
if (await isNameTaken(createAccountData.DisplayName)) {
|
||||||
|
res.status(409).json("Name already in use");
|
||||||
|
} else {
|
||||||
const databaseAccount = toDatabaseAccount(createAccountData);
|
const databaseAccount = toDatabaseAccount(createAccountData);
|
||||||
|
|
||||||
const account = await createAccount(databaseAccount);
|
const account = await createAccount(databaseAccount);
|
||||||
|
|
||||||
res.json(account);
|
res.json(account);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export { createAccountController };
|
export { createAccountController };
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
import { RequestHandler } from "express";
|
import { RequestHandler } from "express";
|
||||||
import { getAccountForRequest } from "@/src/services/loginService";
|
import { getAccountForRequest, isNameTaken } from "@/src/services/loginService";
|
||||||
|
|
||||||
export const renameAccountController: RequestHandler = async (req, res) => {
|
export const renameAccountController: RequestHandler = async (req, res) => {
|
||||||
const account = await getAccountForRequest(req);
|
const account = await getAccountForRequest(req);
|
||||||
if (typeof req.query.newname == "string") {
|
if (typeof req.query.newname == "string") {
|
||||||
|
if (await isNameTaken(req.query.newname)) {
|
||||||
|
res.status(409).json("Name already in use");
|
||||||
|
} else {
|
||||||
account.DisplayName = req.query.newname;
|
account.DisplayName = req.query.newname;
|
||||||
await account.save();
|
await account.save();
|
||||||
res.end();
|
res.end();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
res.status(400).end();
|
res.status(400).end();
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,10 @@ export const isCorrectPassword = (requestPassword: string, databasePassword: str
|
|||||||
return requestPassword === databasePassword;
|
return requestPassword === databasePassword;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const isNameTaken = async (name: string): Promise<boolean> => {
|
||||||
|
return !!(await Account.findOne({ DisplayName: name }));
|
||||||
|
};
|
||||||
|
|
||||||
export const createAccount = async (accountData: IDatabaseAccount): Promise<IDatabaseAccountJson> => {
|
export const createAccount = async (accountData: IDatabaseAccount): Promise<IDatabaseAccountJson> => {
|
||||||
const account = new Account(accountData);
|
const account = new Account(accountData);
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user