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 findByUserId(userId) {
|
|
try {
|
|
const sql = "SELECT * FROM credentials WHERE userId = ?"
|
|
const rows = await pool.query(sql, [userId])
|
|
return rows[0] || null
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function create(userId, passwordHash) {
|
|
try {
|
|
const sql = "INSERT INTO credentials (userId, passwordHash) VALUES (?, ?) RETURNING *"
|
|
const rows = await pool.query(sql, [userId, passwordHash])
|
|
return rows || null
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function updatePassword(userId, passwordHash) {
|
|
try {
|
|
const sql = "UPDATE credentials SET passwordHash = ? WHERE userId = ?"
|
|
const rows = await pool.query(sql, [passwordHash, userId])
|
|
return rows.affectedRows > 0
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function remove(id) {
|
|
try {
|
|
const sql = "DELETE FROM credentials WHERE userId = ?"
|
|
const rows = await pool.query(sql, [id])
|
|
return rows.affectedRows > 0
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
create,
|
|
remove,
|
|
findByUserId,
|
|
updatePassword,
|
|
} |