mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-09-23 15:15:46 +08:00
allowing users to update the casing of their own name
This commit is contained in:
parent
017ca95236
commit
aecdd21474
3 changed files with 21 additions and 5 deletions
|
@ -49,7 +49,7 @@ export async function createNewUser(
|
|||
throw new MonkeyError(400, "Invalid domain");
|
||||
}
|
||||
|
||||
const available = await UserDAL.isNameAvailable(name);
|
||||
const available = await UserDAL.isNameAvailable(name, uid);
|
||||
if (!available) {
|
||||
throw new MonkeyError(409, "Username unavailable");
|
||||
}
|
||||
|
@ -256,8 +256,9 @@ export async function checkName(
|
|||
req: MonkeyTypes.Request
|
||||
): Promise<MonkeyResponse> {
|
||||
const { name } = req.params;
|
||||
const { uid } = req.ctx.decodedToken;
|
||||
|
||||
const available = await UserDAL.isNameAvailable(name);
|
||||
const available = await UserDAL.isNameAvailable(name, uid);
|
||||
if (!available) {
|
||||
throw new MonkeyError(409, "Username unavailable");
|
||||
}
|
||||
|
|
|
@ -119,6 +119,9 @@ router.post(
|
|||
|
||||
router.get(
|
||||
"/checkName/:name",
|
||||
authenticateRequest({
|
||||
isPublic: true,
|
||||
}),
|
||||
RateLimit.userCheckName,
|
||||
validateRequest({
|
||||
params: {
|
||||
|
|
|
@ -90,10 +90,16 @@ export async function updateName(
|
|||
name: string,
|
||||
previousName: string
|
||||
): Promise<void> {
|
||||
if (name === previousName) {
|
||||
throw new MonkeyError(400, "New name is the same as the old name");
|
||||
}
|
||||
if (!isUsernameValid(name)) {
|
||||
throw new MonkeyError(400, "Invalid username");
|
||||
}
|
||||
if (!(await isNameAvailable(name))) {
|
||||
if (
|
||||
name.toLowerCase() !== previousName.toLowerCase() &&
|
||||
!(await isNameAvailable(name, uid))
|
||||
) {
|
||||
throw new MonkeyError(409, "Username already taken", name);
|
||||
}
|
||||
|
||||
|
@ -180,8 +186,14 @@ async function findByName(name: string): Promise<MonkeyTypes.User | undefined> {
|
|||
)[0];
|
||||
}
|
||||
|
||||
export async function isNameAvailable(name: string): Promise<boolean> {
|
||||
return (await findByName(name)) === undefined;
|
||||
export async function isNameAvailable(
|
||||
name: string,
|
||||
uid: string
|
||||
): Promise<boolean> {
|
||||
const user = await findByName(name);
|
||||
// if the user found by name is the same as the user we are checking for, then the name is available
|
||||
// this means that the user can update the casing of their name without it being taken
|
||||
return user === undefined || user.uid === uid;
|
||||
}
|
||||
|
||||
export async function getUserByName(
|
||||
|
|
Loading…
Add table
Reference in a new issue