Add base project files including environment example, license, README, .gitignore, error classes, ESLint config, database modules, texture assets, repositories, routes, schemas, services, and server entry point. This establishes the foundational structure for a Yggdrasil-compatible REST API with modular error handling, database setup, and route organization.
66 lines
2.8 KiB
JavaScript
66 lines
2.8 KiB
JavaScript
const path = require("path")
|
|
const express = require("express")
|
|
const router = express.Router()
|
|
const utils = require("../../../../modules/utils")
|
|
const authService = require("../../../../services/authService")
|
|
const sessionsService = require("../../../../services/sessionsService")
|
|
const userRepository = require("../../../../repositories/userRepository")
|
|
const logger = require("../../../../modules/logger")
|
|
const { SessionError, DefaultError } = require("../../../../errors/errors")
|
|
|
|
router.post("/", async (req, res) => {
|
|
const { accessToken, selectedProfile, serverId } = req.body
|
|
|
|
try {
|
|
const verificationResult = await authService.verifyAccessToken({ accessToken })
|
|
const tokenUuid = verificationResult.user.uuid
|
|
const requestedProfile = utils.addDashesToUUID(selectedProfile)
|
|
|
|
if (tokenUuid !== requestedProfile) {
|
|
throw new SessionError(403, "Forbidden", "You cannot join with a profile that is not yours.", req.originalUrl)
|
|
}
|
|
|
|
const bansResult = await userRepository.getPlayerBans(tokenUuid)
|
|
if (bansResult.code === 200 && bansResult.bans && bansResult.bans.length > 0) {
|
|
const activeBan = bansResult.bans[0]
|
|
throw new SessionError(
|
|
403,
|
|
"UserBannedException",
|
|
activeBan.reasonMessage || "You are banned from multiplayer.",
|
|
req.originalUrl
|
|
)
|
|
}
|
|
|
|
try {
|
|
const privsResult = await userRepository.getPlayerPrivileges(tokenUuid)
|
|
if (privsResult.code === 200 && privsResult.data) {
|
|
if (!privsResult.data.multiplayerServer) {
|
|
throw new SessionError(403, "InsufficientPrivilegesException", "Multiplayer is disabled for your account.", req.originalUrl)
|
|
}
|
|
}
|
|
} catch (privError) {
|
|
if (privError instanceof DefaultError && privError.code !== 404) throw privError
|
|
}
|
|
const ip = req.headers["x-forwarded-for"] || req.socket.remoteAddress
|
|
await sessionsService.joinServer({
|
|
clientToken: verificationResult.session.clientToken,
|
|
accessToken,
|
|
selectedProfile: requestedProfile,
|
|
serverId,
|
|
ip
|
|
})
|
|
logger.log(`Server join success: ${verificationResult.user.username}`, ["SESSION", "green"])
|
|
return res.status(204).end()
|
|
} catch (err) {
|
|
console.log(err)
|
|
if (err instanceof SessionError) throw err
|
|
if (err instanceof DefaultError) {
|
|
const statusCode = err.code === 401 ? 403 : (err.code || 500)
|
|
const errorName = "Forbidden"
|
|
throw new SessionError(statusCode, errorName, err.message, req.originalUrl)
|
|
}
|
|
throw new SessionError(500, "Forbidden", "Internal Server Error", req.originalUrl)
|
|
}
|
|
})
|
|
|
|
module.exports = router |