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:
2026-09-16 01:45:44 +02:00
parent 22542243f6
commit 38882d7b87
17 changed files with 272 additions and 20 deletions
+8 -5
View File
@@ -1,4 +1,5 @@
const jwt = require("jsonwebtoken")
const crypto = require("node:crypto")
const security = require("../modules/security")
const tokenRepo = require("../repositories/refreshTokensRepo")
const { DefaultError } = require("../errors/errors")
@@ -12,7 +13,8 @@ async function signToken(user) {
id: user.id,
identifier: user.identifier,
displayName: user.displayName,
avatarUrl: user.avatarUrl
avatarUrl: user.avatarUrl,
role: user.role
}
const token = await jwt.sign(payload, privateKey, {
algorithm: "RS256",
@@ -31,7 +33,6 @@ async function verifyAccessToken(token) {
algorithms: ["RS256"],
issuer: process.env.FQDN
})
console.log(result)
return result
} catch (error) {
throw error
@@ -56,7 +57,8 @@ async function signRefreshToken(userId) {
expiresIn: expiresIn
}
)
await tokenRepo.create(userId, token, expiresAtInMs)
const hash = crypto.createHash("sha256").update(token).digest("hex")
await tokenRepo.create(userId, hash, expiresAtInMs)
return token
} catch (error) {
throw error
@@ -80,11 +82,12 @@ async function verifyRefreshToken(token) {
if (!result) {
throw new DefaultError(500, "Internal Server Error")
}
const refreshTokenInDb = await tokenRepo.findByTokenHash(token)
const hash = crypto.createHash("sha256").update(token).digest("hex")
const refreshTokenInDb = await tokenRepo.findByTokenHash(hash)
if (refreshTokenInDb == null) {
throw new DefaultError(403, "Bad refresh token", "Refresh token doesn't exists")
}
return true
return result
} catch (error) {
throw error
}