-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { route } from "@spacebar/api"; | ||
import { Config, UniqueUsernameAttemptSchema, User } from "@spacebar/util"; | ||
import { Request, Response, Router } from "express"; | ||
import { HTTPError } from "lambert-server"; | ||
const router = Router(); | ||
|
||
// https://discord-userdoccers.vercel.app/resources/user#get-pomelo-eligibility | ||
router.post( | ||
"/", | ||
route({ | ||
requestBody: "UniqueUsernameAttemptSchema", | ||
responses: { | ||
200: { body: "UniqueUsernameAttemptResponse" }, | ||
400: { body: "APIErrorResponse" }, | ||
}, | ||
description: | ||
"Checks whether a unique username is available for the user to claim.", | ||
}), | ||
async (req: Request, res: Response) => { | ||
const body = req.body as UniqueUsernameAttemptSchema; | ||
const { uniqueUsernames } = Config.get().general; | ||
if (!uniqueUsernames) { | ||
throw new HTTPError( | ||
"Unique Usernames feature is not enabled on this instance.", | ||
400, | ||
); | ||
} | ||
|
||
res.json({ | ||
taken: !(await User.isUsernameAvailable(body.username)), | ||
}); | ||
}, | ||
); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { route } from "@spacebar/api"; | ||
import { Config, User } from "@spacebar/util"; | ||
import { Request, Response, Router } from "express"; | ||
import { HTTPError } from "lambert-server"; | ||
const router = Router(); | ||
|
||
// https://discord-userdoccers.vercel.app/resources/user#get-pomelo-suggestions | ||
router.get( | ||
"/", | ||
route({ | ||
description: | ||
"Returns a suggested unique username string based on the current user's username.", | ||
responses: { | ||
400: { body: "APIErrorResponse" }, | ||
}, | ||
}), | ||
async (req: Request, res: Response) => { | ||
const { uniqueUsernames } = Config.get().general; | ||
if (!uniqueUsernames) { | ||
throw new HTTPError( | ||
"Unique Usernames feature is not enabled on this instance.", | ||
400, | ||
); | ||
} | ||
|
||
const user = await User.findOneOrFail({ | ||
where: { | ||
id: req.user_id, | ||
}, | ||
}); | ||
|
||
// TODO: return a suggestion based on the users current username | ||
return res.json({ username: user.username.toString() }); | ||
}, | ||
); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { route } from "@spacebar/api"; | ||
import { | ||
Config, | ||
FieldErrors, | ||
UniqueUsernameAttemptSchema, | ||
User, | ||
} from "@spacebar/util"; | ||
import { Request, Response, Router } from "express"; | ||
import { HTTPError } from "lambert-server"; | ||
const router = Router(); | ||
|
||
// https://discord-userdoccers.vercel.app/resources/user#create-pomelo-migration | ||
router.post( | ||
"/", | ||
route({ | ||
description: | ||
"Claims a unique username for the user. Returns the updated user object on success. Fires a User Update Gateway event.", | ||
requestBody: "UniqueUsernameAttemptSchema", | ||
responses: { | ||
200: { body: "PrivateUserResponse" }, | ||
400: { body: "APIErrorResponse" }, | ||
}, | ||
}), | ||
async (req: Request, res: Response) => { | ||
const body = req.body as UniqueUsernameAttemptSchema; | ||
const { uniqueUsernames } = Config.get().general; | ||
if (!uniqueUsernames) { | ||
throw new HTTPError( | ||
"Unique Usernames feature is not enabled on this instance.", | ||
400, | ||
); | ||
} | ||
|
||
const isAvailable = await User.isUsernameAvailable(body.username); | ||
|
||
if (!isAvailable) { | ||
throw FieldErrors({ | ||
username: { | ||
code: "USERNAME_TOO_MANY_USERS", | ||
message: | ||
req?.t("auth:register.USERNAME_TOO_MANY_USERS") || "", | ||
}, | ||
}); | ||
} | ||
|
||
const user = await User.findOneOrFail({ | ||
where: { | ||
id: req.user_id, | ||
}, | ||
}); | ||
|
||
user.legacy_username = user.username; | ||
user.username = body.username; | ||
user.discriminator = "0"; | ||
const newUser = await user.save(); | ||
|
||
res.json(newUser.toPrivateUser()); | ||
}, | ||
); | ||
|
||
export default router; |