Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const { username, password } = ctx.request.body
const email = ctx.request.body.email.trim()
if (!isValidUsername(username) || !isValidEmail(email) || !isValidPassword(password)) {
throw new httpErrors.BadRequest('Invalid parameters')
}
const hashed = await hashPass(password)
let result
try {
const user = users.create(username, email, hashed, ctx.ip)
result = await user.save()
} catch (err) {
if (err.code && err.code === UNIQUE_VIOLATION) {
throw new httpErrors.Conflict('A user with that name already exists')
}
throw err
}
// regenerate the session to ensure that logged in sessions and anonymous sessions don't
// share a session ID
await ctx.regenerateSession()
initSession(ctx, result.user, result.permissions)
setReturningCookie(ctx)
const code = cuid()
await addEmailVerificationCode(result.user.id, email, code, ctx.ip)
await sendAccountVerificationEmail(code, result.user.id, email)
ctx.body = result
}
const userId = +ctx.params.userId
const { banLengthHours, reason } = ctx.request.body
if (isNaN(userId)) {
throw new httpErrors.BadRequest('User ID must be an integer')
}
if (!banLengthHours) {
throw new httpErrors.BadRequest('Ban length must be specified')
}
const user = await users.find(userId)
if (user === null) {
throw new httpErrors.NotFound('User does not exist')
}
if (userId === ctx.session.userId) {
throw new httpErrors.Conflict("Can't ban yourself")
}
try {
const ban = await dbBanUser(userId, ctx.session.userId, banLengthHours, reason)
ctx.body = {
startTime: +ban.startTime,
endTime: +ban.endTime,
bannedBy: ban.bannedBy,
reason: ban.reason,
}
} catch (err) {
ctx.log.error({ err }, 'error banning the user')
throw err
}
// Clear all existing sessions for this user
const userSessionsKey = 'user_sessions:' + userId
ensureLobbyLoading(lobby) {
if (!this.loadingLobbies.has(lobby.name)) {
throw new errors.Conflict('lobby must be loading')
}
}
async function endSession(ctx, next) {
if (!ctx.session.userId) {
throw new httpErrors.Conflict('No session active')
}
await redis.srem('user_sessions:' + ctx.session.userId, ctx.sessionId)
await ctx.regenerateSession()
ctx.status = 204
}
ensureLobbyNotTransient(lobby) {
if (this.lobbyCountdowns.has(lobby.name)) {
throw new errors.Conflict('lobby is counting down')
}
if (this.loadingLobbies.has(lobby.name)) {
throw new errors.Conflict('lobby has already started')
}
}
ensureLobbyNotTransient(lobby) {
if (this.lobbyCountdowns.has(lobby.name)) {
throw new errors.Conflict('lobby is counting down')
}
if (this.loadingLobbies.has(lobby.name)) {
throw new errors.Conflict('lobby has already started')
}
}
if (!this.lobbies.has(name)) {
throw new errors.NotFound('no lobby found with that name')
}
const lobby = this.lobbies.get(name)
this.ensureLobbyNotTransient(lobby)
if (
this.lobbyBannedUsers.has(lobby.name) &&
this.lobbyBannedUsers.get(lobby.name).includes(client.name)
) {
throw new errors.Conflict('user has been banned from this lobby')
}
const [teamIndex, slotIndex, availableSlot] = Lobbies.findAvailableSlot(lobby)
if (teamIndex < 0 || slotIndex < 0) {
throw new errors.Conflict('lobby is full')
}
let player
const [, observerTeam] = getObserverTeam(lobby)
if (observerTeam && observerTeam.slots.find(s => s.id === availableSlot.id)) {
player = Slots.createObserver(client.name)
} else {
player = isUms(lobby.gameType)
? Slots.createHuman(client.name, availableSlot.race, true, availableSlot.playerId)
: Slots.createHuman(client.name)
}
const updated = Lobbies.addPlayer(lobby, teamIndex, slotIndex, player)
if (!activityRegistry.registerActiveClient(user.name, client)) {
throw new errors.Conflict('user is already active in a gameplay activity')
ensureLobbyNotLoading(lobby) {
if (this.loadingLobbies.has(lobby.name)) {
throw new errors.Conflict('lobby has already started')
}
}