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:
@@ -2,7 +2,6 @@ const logger = require("../modules/logger")
|
||||
const bcrypt = require("bcryptjs")
|
||||
const database = require("../modules/database")
|
||||
const { DefaultError } = require("../errors/errors")
|
||||
const usernameRegex = /^[a-zA-Z0-9_]{3,16}$/
|
||||
|
||||
async function getUser(identifier, requirePassword = false) {
|
||||
try {
|
||||
@@ -28,19 +27,14 @@ async function getUser(identifier, requirePassword = false) {
|
||||
|
||||
async function register(email, username, password) {
|
||||
try {
|
||||
const availability = await checkUsernameAvailability(username)
|
||||
if (availability.allowed) {
|
||||
const sql = `INSERT INTO players (email, username, password, uuid) VALUES (?, ?, ?, ?)`
|
||||
const uuid = crypto.randomUUID()
|
||||
const hashedPassword = await bcrypt.hash(password, 10)
|
||||
const result = await database.query(sql, [email, username, hashedPassword, uuid])
|
||||
if (result.affectedRows > 0) {
|
||||
return { code: 200, email, username, uuid }
|
||||
} else {
|
||||
throw new DefaultError(500, "Please contact an administrator.", "InternalServerError")
|
||||
}
|
||||
const sql = `INSERT INTO players (email, username, password, uuid) VALUES (?, ?, ?, ?)`
|
||||
const uuid = crypto.randomUUID()
|
||||
const hashedPassword = await bcrypt.hash(password, 10)
|
||||
const result = await database.query(sql, [email, username, hashedPassword, uuid])
|
||||
if (result.affectedRows > 0) {
|
||||
return { code: 200, email, username, uuid }
|
||||
} else {
|
||||
throw new DefaultError(415, "Illegal Server Character", availability.message || "INVALID_USERNAME_FORMAT")
|
||||
throw new DefaultError(500, "Please contact an administrator.", "InternalServerError")
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof DefaultError) throw error
|
||||
@@ -51,30 +45,6 @@ async function register(email, username, password) {
|
||||
}
|
||||
}
|
||||
|
||||
async function checkUsernameAvailability(username) {
|
||||
if (!usernameRegex.test(username)) {
|
||||
return { code: 200, allowed: false, message: "Invalid format (3-16 alphanumeric chars)." }
|
||||
}
|
||||
|
||||
const blocklist = await getUsernamesRules()
|
||||
const normalizedUsername = username.toLowerCase()
|
||||
|
||||
for (const entry of blocklist) {
|
||||
if (entry.type === "literal") {
|
||||
if (normalizedUsername === entry.value) {
|
||||
return { code: 200, allowed: false, message: "This username is reserved." }
|
||||
}
|
||||
}
|
||||
else if (entry.type === "regex") {
|
||||
if (entry.pattern.test(username)) {
|
||||
return { code: 200, allowed: false, message: "This username contains forbidden patterns." }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { code: 200, allowed: true }
|
||||
}
|
||||
|
||||
async function insertClientSession(accessToken, clientToken, uuid) {
|
||||
try {
|
||||
const sql = `INSERT INTO clientSessions (accessToken, clientToken, uuid) VALUES (?, ?, ?)`
|
||||
@@ -228,6 +198,7 @@ module.exports = {
|
||||
getUser,
|
||||
register,
|
||||
getClientSession,
|
||||
getUsernamesRules,
|
||||
revokeAccessTokens,
|
||||
insertClientSession,
|
||||
getPlayerProperties,
|
||||
|
||||
Reference in New Issue
Block a user