Add admin login and password change endpoints
Introduces POST /login and PATCH /password routes for admin authentication and password management. Adds corresponding schema validation for login and password change, enforces stricter password requirements, and updates adminService with JWT-based profile retrieval and improved token handling.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
const jwt = require("jsonwebtoken")
|
||||
const bcrypt = require("bcryptjs")
|
||||
const userRepository = require("../repositories/userRepository")
|
||||
const adminRepository = require("../repositories/adminRepository")
|
||||
const bcrypt = require("bcryptjs")
|
||||
const { DefaultError } = require("../errors/errors")
|
||||
|
||||
const ADMIN_JWT_SECRET = process.env.ADMIN_JWT_SECRET || "udjJLGCOq7m3NmGpdVLJ@#"
|
||||
@@ -28,7 +29,7 @@ async function checkAdminAccess(adminId, requiredPermission) {
|
||||
}
|
||||
|
||||
async function changeAdminPassword(adminId, newPlainPassword) {
|
||||
if (!newPlainPassword || newPlainPassword.length < 6) {
|
||||
if (!newPlainPassword || newPlainPassword.length < 8) {
|
||||
throw new DefaultError(400, "Le mot de passe doit contenir au moins 6 caractères.")
|
||||
}
|
||||
|
||||
@@ -52,6 +53,15 @@ async function getAdminProfile(adminId) {
|
||||
}
|
||||
}
|
||||
|
||||
async function getAdminProfileByToken(accessToken) {
|
||||
try {
|
||||
const decoded = jwt.verify(accessToken, { complete: true, json: true })
|
||||
return getAdminProfile(decoded.sub)
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async function grantPermission(adminId, permissionKey) {
|
||||
return await adminRepository.assignPermission(adminId, permissionKey)
|
||||
}
|
||||
@@ -74,7 +84,7 @@ async function loginAdmin(username, password) {
|
||||
const token = jwt.sign(
|
||||
{ id: admin.id, username: admin.username, type: "admin" },
|
||||
ADMIN_JWT_SECRET,
|
||||
{ expiresIn: "8h" }
|
||||
{ expiresIn: "8h", subject: admin.id, issuer: "Yggdrasil" }
|
||||
)
|
||||
|
||||
return { token }
|
||||
@@ -147,5 +157,6 @@ module.exports = {
|
||||
logPlayerAction,
|
||||
revokePermission,
|
||||
checkAdminAccess,
|
||||
changeAdminPassword
|
||||
changeAdminPassword,
|
||||
getAdminProfileByToken
|
||||
}
|
||||
Reference in New Issue
Block a user