generated from azures04/Base-REST-API
Introduce user bans and caching, extend schemas, and add auth/user endpoints. Adds bans DDL and banRepo plus adminService to manage bans; introduces modules/cache for in-memory user caching used by authService and userService. Updates users and credentials DDLs (role, isDisabled, shouldReset). Changes tokensService to include role in access tokens and store refresh tokens as SHA-256 hashes. Updates repos (credentials create RETURNING, usersRepo.updateProfile positional params), enhances authService with isLogged/isNotLogged middleware, ban checks, and refreshed token flow. Adds auth routes (login, logout, refresh, register) and user routes (/me).
107 lines
3.0 KiB
JavaScript
107 lines
3.0 KiB
JavaScript
const { pool } = require("../modules/database")
|
|
const { DefaultError } = require("../errors/errors")
|
|
|
|
async function findById(id) {
|
|
try {
|
|
const sql = "SELECT * FROM users WHERE id = ?"
|
|
const rows = await pool.query(sql, [id])
|
|
return rows[0] || null
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function findByIdentifier(identifier) {
|
|
try {
|
|
const sql = "SELECT * FROM users WHERE identifier = ?"
|
|
const rows = await pool.query(sql, [identifier])
|
|
return rows[0] || null
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function create(identifier, serverId, remoteId = null, displayName, avatarUrl) {
|
|
try {
|
|
const sql = "INSERT INTO users (identifier, serverId, remoteId, displayName, avatarUrl) VALUES (?, ?, ?, ?, ?) RETURNING *"
|
|
const rows = await pool.query(sql, [identifier, serverId, remoteId, displayName, avatarUrl])
|
|
return rows[0] || null
|
|
} catch (error) {
|
|
if (error.code && error.code == "ER_DUP_ENTRY") {
|
|
throw new DefaultError(401, "Identifier already taken.", "Identifier already assigned.")
|
|
}
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function remove(id) {
|
|
try {
|
|
const sql = "DELETE FROM users WHERE id = ?"
|
|
const rows = await pool.query(sql, [id])
|
|
return rows.affectedRows > 0
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function isLocal(id) {
|
|
const sql = "SELECT (remoteId IS NULL) AS isLocal FROM users WHERE id = ?"
|
|
const rows = await db.query(sql, [id])
|
|
|
|
if (rows.length === 0) {
|
|
throw new DefaultError(404, `User ${id} not found`)
|
|
}
|
|
|
|
return Boolean(rows[0].isLocal)
|
|
}
|
|
|
|
async function updateProfile(id, displayName, avatarUrl) {
|
|
try {
|
|
const fields = []
|
|
const params = []
|
|
|
|
if (displayName !== undefined) {
|
|
fields.push('`displayName` = ?')
|
|
params.push(displayName)
|
|
}
|
|
|
|
if (avatarUrl !== undefined) {
|
|
fields.push('`avatarUrl` = ?')
|
|
params.push(avatarUrl)
|
|
}
|
|
|
|
if (fields.length === 0) {
|
|
return findById(id)
|
|
}
|
|
|
|
params.push(id)
|
|
|
|
const sql = `UPDATE users SET ${fields.join(', ')} WHERE id = ?`
|
|
await pool.query(sql, params)
|
|
|
|
return findById(id)
|
|
} catch (error) {
|
|
console.error(error)
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function findByServerAndRemoteId(serverId, remoteId) {
|
|
try {
|
|
const sql = "SELECT * FROM users WHERE serverId = ? AND remoteId = ?"
|
|
const rows = await pool.query(sql, [serverId, remoteId])
|
|
return rows[0] || null
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
create,
|
|
remove,
|
|
isLocal,
|
|
findById,
|
|
updateProfile,
|
|
findByIdentifier,
|
|
findByServerAndRemoteId
|
|
} |