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.
44 lines
1.8 KiB
JavaScript
44 lines
1.8 KiB
JavaScript
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 |