generated from azures04/Base-REST-API
Add bans, user cache, auth routes & token changes
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).
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
const { pool } = require("../modules/database")
|
||||
const { DefaultError } = require("../errors/errors")
|
||||
|
||||
async function banUser(userId, message, expiresAt) {
|
||||
try {
|
||||
const sql = "INSERT INTO bans (userId, message, expiresAt) VALUES (?, ?, ?) RETURNING *"
|
||||
const rows = await pool.query(sql, [userId, message, expiresAt])
|
||||
return rows[0] || null
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function isBanned(userId) {
|
||||
try {
|
||||
const sql = "SELECT * FROM bans WHERE userId = ? AND isActive = TRUE LIMIT 1"
|
||||
const rows = await pool.query(sql, [userId])
|
||||
return rows[0] || null
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function unbanUser(banId) {
|
||||
try {
|
||||
const sql = "UPDATE bans SET isActive = FALSE WHERE id = ?"
|
||||
const rows = await pool.query(sql, [banId])
|
||||
return rows.affectedRows > 0
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function getBanHistory(userId) {
|
||||
try {
|
||||
const sql = "SELECT * FROM bans WHERE id = userId"
|
||||
const rows = await pool.query(sql, [userId, limit])
|
||||
return rows.affectedRows > 0
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
banUser,
|
||||
isBanned,
|
||||
unbanUser,
|
||||
getBanHistory
|
||||
}
|
||||
@@ -13,7 +13,7 @@ async function findByUserId(userId) {
|
||||
|
||||
async function create(userId, passwordHash) {
|
||||
try {
|
||||
const sql = "INSERT INTO credentials (userId, passwordHash) VALUES (?, ?)"
|
||||
const sql = "INSERT INTO credentials (userId, passwordHash) VALUES (?, ?) RETURNING *"
|
||||
const rows = await pool.query(sql, [userId, passwordHash])
|
||||
return rows || null
|
||||
} catch (error) {
|
||||
|
||||
@@ -58,27 +58,30 @@ async function isLocal(id) {
|
||||
async function updateProfile(id, displayName, avatarUrl) {
|
||||
try {
|
||||
const fields = []
|
||||
const params = { id }
|
||||
const params = []
|
||||
|
||||
if (displayName !== undefined) {
|
||||
fields.push('`displayName` = :displayName')
|
||||
params.displayName = displayName
|
||||
fields.push('`displayName` = ?')
|
||||
params.push(displayName)
|
||||
}
|
||||
|
||||
if (avatarUrl !== undefined) {
|
||||
fields.push('`avatarUrl` = :avatarUrl')
|
||||
params.avatarUrl = avatarUrl
|
||||
fields.push('`avatarUrl` = ?')
|
||||
params.push(avatarUrl)
|
||||
}
|
||||
|
||||
if (fields.length === 0) {
|
||||
return findById(id)
|
||||
}
|
||||
|
||||
const sql = `UPDATE users SET ${fields.join(', ')} WHERE id = :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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user