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 { 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 { DTLS, groups, HUB, platformCDNs } from "@/static/fixed_responses/login_static";
|
||||
import { logger } from "@/src/utils/logger";
|
||||
@ -26,10 +26,19 @@ export const loginController: RequestHandler = async (request, response) => {
|
||||
|
||||
if (!account && config.autoCreateAccount && loginRequest.ClientType != "webui") {
|
||||
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({
|
||||
email: loginRequest.email,
|
||||
password: loginRequest.password,
|
||||
DisplayName: loginRequest.email.substring(0, loginRequest.email.indexOf("@")),
|
||||
DisplayName: name,
|
||||
CountryCode: loginRequest.lang.toUpperCase(),
|
||||
ClientType: loginRequest.ClientType,
|
||||
CrossPlatformAllowed: true,
|
||||
|
@ -1,14 +1,16 @@
|
||||
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";
|
||||
|
||||
const createAccountController: RequestHandler = async (req, res) => {
|
||||
const createAccountData = toCreateAccount(req.body);
|
||||
const databaseAccount = toDatabaseAccount(createAccountData);
|
||||
|
||||
const account = await createAccount(databaseAccount);
|
||||
|
||||
res.json(account);
|
||||
if (await isNameTaken(createAccountData.DisplayName)) {
|
||||
res.status(409).json("Name already in use");
|
||||
} else {
|
||||
const databaseAccount = toDatabaseAccount(createAccountData);
|
||||
const account = await createAccount(databaseAccount);
|
||||
res.json(account);
|
||||
}
|
||||
};
|
||||
|
||||
export { createAccountController };
|
||||
|
@ -1,12 +1,16 @@
|
||||
import { RequestHandler } from "express";
|
||||
import { getAccountForRequest } from "@/src/services/loginService";
|
||||
import { getAccountForRequest, isNameTaken } from "@/src/services/loginService";
|
||||
|
||||
export const renameAccountController: RequestHandler = async (req, res) => {
|
||||
const account = await getAccountForRequest(req);
|
||||
if (typeof req.query.newname == "string") {
|
||||
account.DisplayName = req.query.newname;
|
||||
await account.save();
|
||||
res.end();
|
||||
if (await isNameTaken(req.query.newname)) {
|
||||
res.status(409).json("Name already in use");
|
||||
} else {
|
||||
account.DisplayName = req.query.newname;
|
||||
await account.save();
|
||||
res.end();
|
||||
}
|
||||
} else {
|
||||
res.status(400).end();
|
||||
}
|
||||
|
@ -12,6 +12,10 @@ export const isCorrectPassword = (requestPassword: string, databasePassword: str
|
||||
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> => {
|
||||
const account = new Account(accountData);
|
||||
try {
|
||||
|
Loading…
x
Reference in New Issue
Block a user