1
0
Fork 0
nodejs-backend-template/server/api/public/controllers/refresh/admin-refresh.js

33 lines
1.2 KiB
JavaScript

const {Admin} = require('../../../../dao/models');
const Joi = require('joi');
const ApplicationError = require('../../../../utils/application-error');
/**
* @module An object to authenticate the administrator.
*
* @property {string} method - HTTP method (post).
* @property {string} path - The path to process the request.
* @property {Joi.ObjectSchema} validationSchema - Input validation scheme.
* @property {Array} middlewares - Middlewares that can be applied to the request processing (empty array).
* @property {Function} handler - The function that handles the request.
* @async
* @param {Object} req - The request object.
* @param {Object} res - Response object.
* @returns {Object} - An object that contains information.
* @throws {Error} - An exception if an error occurred while processing the request.
*/
module.exports = {
method: 'post',
path: '/refresh/admin',
validationSchema: Joi.object({
refreshToken: Joi.string().required(),
}),
middlewares: [],
handler: async function (req, res) {
const admin = await Admin.scope('auth').findOne({where: {refreshToken: req.body.refreshToken}});
if (!admin) {
throw ApplicationError.NotFound();
}
return await admin.authenticateByRefresh();
}
};