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:
@@ -0,0 +1,14 @@
|
||||
const express = require("express")
|
||||
const authService = require("../../../../services/authService")
|
||||
const router = express.Router()
|
||||
|
||||
router.post("/", authService.isNotLogged, async (req, res) => {
|
||||
if (process.env.LOGIN_ENABLED != "true") {
|
||||
return res.status(401).json({ error: "login_disabled", error_description: "The administrator has disabled internal login." })
|
||||
}
|
||||
const { identifier, password } = req.body
|
||||
const user = await authService.loginLocal({ identifier, password })
|
||||
return res.status(201).json(user)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,12 @@
|
||||
const express = require("express")
|
||||
const authService = require("../../../../services/authService")
|
||||
const tokensService = require("../../../../services/tokensService")
|
||||
const router = express.Router()
|
||||
|
||||
router.post("/", authService.isLogged, async (req, res) => {
|
||||
const { refresh_token } = req.body
|
||||
await tokensService.revokeRefreshToken(refresh_token)
|
||||
return res.status(200).json(user)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,11 @@
|
||||
const express = require("express")
|
||||
const authService = require("../../../../services/authService")
|
||||
const router = express.Router()
|
||||
|
||||
router.post("/", authService.isLogged, async (req, res) => {
|
||||
const { refresh_token } = req.body
|
||||
const tokens = await authService.refreshAccessToken({ refreshToken: refresh_token })
|
||||
return res.status(200).json(tokens)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,14 @@
|
||||
const express = require("express")
|
||||
const authService = require("../../../../services/authService")
|
||||
const router = express.Router()
|
||||
|
||||
router.post("/", authService.isNotLogged, async (req, res) => {
|
||||
if (process.env.REGISTER_ENABLED != "true") {
|
||||
return res.status(401).json({ error: "registration_disabled", error_description: "The administrator has disabled internal registration." })
|
||||
}
|
||||
const { identifier, password, avatarURL, displayName } = req.body
|
||||
const user = await authService.registerLocal({ identifier, password, profile: { avatarURL, displayName } })
|
||||
return res.status(201).json(user)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,7 @@
|
||||
const express = require("express")
|
||||
const authService = require("../../../../services/authService")
|
||||
const router = express.Router()
|
||||
|
||||
router.use("", authService.isLogged)
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,22 @@
|
||||
const express = require("express")
|
||||
const userService = require("../../../../services/userService")
|
||||
const router = express.Router()
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
const user = await userService.getProfile({ userId: req.user.id })
|
||||
return res.status(200).json(user)
|
||||
})
|
||||
|
||||
router.patch("/avatar", async (req, res) => {
|
||||
const { avatarURL } = req.body
|
||||
const updatedProfile = await userService.updateProfile({ userId: req.user.id, profile: { avatarURL } })
|
||||
return res.status(200).json(updatedProfile)
|
||||
})
|
||||
|
||||
router.patch("/displayname", async (req, res) => {
|
||||
const { displayName } = req.body
|
||||
const updatedProfile = await userService.updateProfile({ userId: req.user.id, profile: { displayName } })
|
||||
return res.status(200).json(updatedProfile)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
Reference in New Issue
Block a user