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

33 lines
1.3 KiB
JavaScript

const {User} = require('../../../../dao/models');
const Joi = require('joi');
const ApplicationError = require('../../../../utils/application-error');
/**
* @module An object representing the HTTP Post method for updating the user's access token.
* @type {Object}
* @property {string} method - HTTP method (post).
* @property {string} path - Path to process the request ("/refresh").
* @property {Joi.ObjectSchema} validationSchema - Input validation scheme.
* @property {Array} middlewares - Middleware that can be applied to the request processing (empty array).
* @property {Function} handler - The function that handles the request to update the user's access token.
* @async
* @param {Object} req - The request object.
* @param {Object} res - Response object.
* @returns {Object} - An object containing the new access token after the update.
* @throws {Error} - Exception if an error occurred while updating the access token.
*/
module.exports = {
method: 'post',
path: '/refresh',
validationSchema: Joi.object({
refreshToken: Joi.string().required(),
}),
middlewares: [],
handler: async function (req, res) {
const user = await User.scope('auth').findOne({where: {refreshToken: req.body.refreshToken}});
if (!user) {
throw ApplicationError.NotFound();
}
return await user.authenticateByRefresh();
}
};