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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user