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).
38 lines
970 B
JavaScript
38 lines
970 B
JavaScript
const { DefaultError } = require("../errors/errors")
|
|
const banRepo = require("../repositories/banRepo")
|
|
|
|
async function banUser(userId, message, expiresAt) {
|
|
const existingBan = await banRepo.isBanned(userId)
|
|
if (existingBan) {
|
|
throw new DefaultError(409, "Already has an active ban.", "Duplicate active ban.")
|
|
}
|
|
return await banRepo.banUser(userId, message, expiresAt)
|
|
}
|
|
|
|
async function unbanUser(banId) {
|
|
const existingBan = await banRepo.isBanned(userId)
|
|
if (!existingBan) {
|
|
throw new DefaultError(404, "No active ban.", "No active ban found.")
|
|
}
|
|
return await banRepo.unbanUser(banId)
|
|
}
|
|
|
|
|
|
async function isUserBanned(userId) {
|
|
const ban = await banRepo.isBanned(userId)
|
|
if (!ban) {
|
|
return false
|
|
}
|
|
|
|
if (new Date(ban.expiresAt) <= new Date()) {
|
|
await banRepo.deactivate(ban.id)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
module.exports = {
|
|
banUser,
|
|
unbanUser,
|
|
isUserBanned
|
|
} |