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).
49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
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
|
|
} |