Add Minecraft services API routes and user service
Introduces new routes under /minecraftservices and /mojangapi for profile, skin, cape, blocklist, privileges, and certificate management. Adds a comprehensive userService module to handle user-related operations, and extends userRepository with methods for username changes, skin/cape management, blocking, and profile lookups. Refactors username availability logic into authService, updates error handling, and improves logger and utility functions. Also updates route handlers to use consistent return statements and enhances route registration logging.
This commit is contained in:
26
routes/minecraftservices/minecraft/profile/capes/active.js
Normal file
26
routes/minecraftservices/minecraft/profile/capes/active.js
Normal file
@@ -0,0 +1,26 @@
|
||||
const express = require("express")
|
||||
const router = express.Router()
|
||||
const userService = require("../../../../../services/userService")
|
||||
const authService = require("../../../../../services/authService")
|
||||
|
||||
router.delete("/", async (req, res) => {
|
||||
const player = await authService.verifyAccessToken({ accessToken: req.headers.authorization })
|
||||
await userService.hideCape(player.user.uuid)
|
||||
return res.status(200).send()
|
||||
})
|
||||
|
||||
router.put("/", async (req, res) => {
|
||||
const player = await authService.verifyAccessToken(req.headers.authorization)
|
||||
|
||||
await userService.showCape(player.user.uuid, req.body.capeId)
|
||||
const [skinsResult, capesResult] = await Promise.all([userService.getSkins(player.user.uuid), userService.getCapes(player.user.uuid)])
|
||||
|
||||
return res.status(200).json({
|
||||
id: player.user.uuid.replace(/-/g, ""),
|
||||
name: player.user.username,
|
||||
skins: skinsResult.data || [],
|
||||
capes: capesResult.data || []
|
||||
})
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
18
routes/minecraftservices/minecraft/profile/index.js
Normal file
18
routes/minecraftservices/minecraft/profile/index.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const express = require("express")
|
||||
const router = express.Router()
|
||||
const userService = require("../../../../services/userService")
|
||||
const authService = require("../../../../services/authService")
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
const player = await authService.verifyAccessToken({ accessToken: req.headers.authorization.replace("Bearer ", "") })
|
||||
const [skinsResult, capesResult] = await Promise.all([userService.getSkins(player.user.uuid), userService.getCapes(player.user.uuid)])
|
||||
|
||||
return res.status(200).json({
|
||||
id: player.uuid.replace(/-/g, ""),
|
||||
name: player.user.username,
|
||||
skins: skinsResult.data || [],
|
||||
capes: capesResult.data || []
|
||||
})
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,10 @@
|
||||
const express = require("express")
|
||||
const userService = require("../../../../../../services/userService")
|
||||
const router = express.Router()
|
||||
|
||||
router.post("/", async (req, res) => {
|
||||
const profiles = await userService.bulkLookup(req.body)
|
||||
return res.status(200).json(profiles)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,27 @@
|
||||
const express = require("express")
|
||||
const utils = require("../../../../../../modules/utils")
|
||||
const userService = require("../../../../../../services/userService")
|
||||
const authService = require("../../../../../../services/authService")
|
||||
const { ServiceError } = require("../../../../../../errors/errors")
|
||||
const router = express.Router({ mergeParams: true })
|
||||
|
||||
router.get("", async (req, res) => {
|
||||
const profile = await userService.getLegacyProfile(req.params.username)
|
||||
const isUsernameOK = await authService.checkUsernameAvailability(newName)
|
||||
const at = req.query.at
|
||||
if (at != undefined && utils.isTrueFromDotEnv("SUPPORT_UUID_TO_NAME_HISTORY")) {
|
||||
const history = await userService.getNameUUIDs(parseInt(at))
|
||||
return res.status(history.code).json(history.data)
|
||||
} else {
|
||||
throw new ServiceError(400, req.originalUrl, "IllegalArgumentException", "Invalid timestamp.")
|
||||
}
|
||||
if (isUsernameOK.status != "AVAILABLE") {
|
||||
throw new ServiceError(400, req.originalUrl, "CONSTRAINT_VIOLATION", "Invalid username.")
|
||||
}
|
||||
if (!profile) {
|
||||
return res.status(204).send()
|
||||
}
|
||||
return res.status(200).json(profile)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
44
routes/minecraftservices/minecraft/profile/name/[name].js
Normal file
44
routes/minecraftservices/minecraft/profile/name/[name].js
Normal file
@@ -0,0 +1,44 @@
|
||||
const express = require("express")
|
||||
const authService = require("../../../../../services/authService")
|
||||
const { DefaultError, ServiceError } = require("../../../../../errors/errors")
|
||||
const router = express.Router({ mergeParams: true })
|
||||
|
||||
router.get("/available", async (req, res) => {
|
||||
try {
|
||||
await authService.verifyAccessToken({ accessToken: req.headers.authorization.replace("Bearer", "").trim() })
|
||||
const isAvailable = await authService.checkUsernameAvailability(req.params.name)
|
||||
return res.status(200).json({ status: isAvailable.status })
|
||||
} catch (error) {
|
||||
if (error instanceof DefaultError) {
|
||||
throw new ServiceError(error.code, req.originalUrl, null, null, null)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
})
|
||||
|
||||
router.put("/", async (req, res) => {
|
||||
try {
|
||||
const player = await authService.verifyAccessToken({ accessToken: req.headers.authorization.replace("Bearer", "").trim() })
|
||||
const newName = req.params.name
|
||||
|
||||
await userService.changeUsername(player.uuid, newName)
|
||||
|
||||
const skinsResult = await userService.getSkins({ uuid: player.uuid })
|
||||
const capesResult = await userService.getCapes({ uuid: player.uuid })
|
||||
|
||||
return res.status(200).json({
|
||||
id: player.uuid.replace(/-/g, ""),
|
||||
name: newName,
|
||||
skins: skinsResult.data || [],
|
||||
capes: capesResult.data || []
|
||||
})
|
||||
|
||||
} catch (err) {
|
||||
const mcStatus = err.code === 409 ? "DUPLICATE" : (err.code === 400 || err.code === 403) ? "NOT_ALLOWED" : null
|
||||
const finalCode = (mcStatus === "DUPLICATE") ? 403 : (err.code || 500)
|
||||
const errorType = mcStatus ? "FORBIDDEN" : (err.error || "Internal Server Error")
|
||||
throw new ServiceError(finalCode, req.originalUrl, errorType, err.message, mcStatus ? { status: mcStatus } : null)
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
12
routes/minecraftservices/minecraft/profile/namechange.js
Normal file
12
routes/minecraftservices/minecraft/profile/namechange.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const express = require("express")
|
||||
const router = express.Router()
|
||||
const userService = require("../../../../services/userService")
|
||||
const authService = require("../../../../services/authService")
|
||||
|
||||
router.put("/", async (req, res) => {
|
||||
const player = await authService.verifyAccessToken({ accessToken: req.headers.authorization.replace("Bearer ", "") })
|
||||
const nameChangeInformation = await userService.getPlayerNameChangeStatus(player.user.uuid)
|
||||
return res.status(nameChangeInformation.code).json(nameChangeInformation.data)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
12
routes/minecraftservices/minecraft/profile/skins/active.js
Normal file
12
routes/minecraftservices/minecraft/profile/skins/active.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const express = require("express")
|
||||
const router = express.Router()
|
||||
const userService = require("../../../../../services/userService")
|
||||
const authService = require("../../../../../services/authService")
|
||||
|
||||
router.delete("/", async (req, res) => {
|
||||
const player = await authService.verifyAccessToken({ accessToken: req.headers.authorization })
|
||||
await userService.resetSkin(player.user.uuid)
|
||||
return res.status(200).send()
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
Reference in New Issue
Block a user