tested and fixed
This commit is contained in:
		@@ -1,4 +1,3 @@
 | 
			
		||||
import log from '../utils/log.js'
 | 
			
		||||
import jwt from 'jsonwebtoken';
 | 
			
		||||
import config from '../../config.json' with {type: "json"};
 | 
			
		||||
import GroupService from '../services/group.js';
 | 
			
		||||
@@ -26,10 +25,15 @@ const requirePassword = async (req, res, next) => {
 | 
			
		||||
 | 
			
		||||
const authenticate = async (req, res, next) => {
 | 
			
		||||
    if (req.method == "OPTIONS") next();
 | 
			
		||||
 | 
			
		||||
    if (!req.headers.authorization) throw new customError(`authenticate no authorization header`, responseCodes.responses.authentication.not_found);
 | 
			
		||||
 | 
			
		||||
    const token = req.headers.authorization.split(' ')[1];
 | 
			
		||||
    if (!token) throw new customError(`authenticate no authorization token in header`, responseCodes.responses.authentication.not_found);
 | 
			
		||||
 | 
			
		||||
    let user = jwt.decode(token, config.secret);
 | 
			
		||||
 | 
			
		||||
    await UserService.getByUsername(user.login.username)
 | 
			
		||||
 | 
			
		||||
    if (!jwt.verify(token, config.secret)) throw new customError(`authenticate token is invalid`, responseCodes.responses.authentication.invalid);
 | 
			
		||||
 | 
			
		||||
    next();
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										21
									
								
								src/middlewares/existance.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/middlewares/existance.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
import customError from '../response/customError.js';
 | 
			
		||||
import responseCodes from '../response/responseCodes.js';
 | 
			
		||||
import GroupService from '../services/group.js';
 | 
			
		||||
 | 
			
		||||
const groupExists = async (req, res, next) => {
 | 
			
		||||
    let groupId = req.params.groupId || req.body.groupId;
 | 
			
		||||
 | 
			
		||||
    let group = await GroupService.getById(groupId);
 | 
			
		||||
 | 
			
		||||
    if (!group) throw new customError(`group does not exists`, responseCodes.responses.groups.id_not_found);
 | 
			
		||||
    next();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const localIdExists = async (req, res, next) => {
 | 
			
		||||
    let localId = req.params.localId || req.body.localId;
 | 
			
		||||
 | 
			
		||||
    if (!localId) throw new customError(`local id is not specified`, responseCodes.responses.general.invalid_syntax);
 | 
			
		||||
    next();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default { groupExists, localIdExists }
 | 
			
		||||
		Reference in New Issue
	
	Block a user