import { Request , Response , NextFunction } from "express" ; import { getRepository } from "typeorm" ; import { User } from "../entity/User" ; export const checkRole = ( roles ) => { return async ( req , res , next ) => { const id = res . locals . jwtPayload . userId ; const userRepository = getRepository ( User ) ; let user ; try { user = await userRepository . findOneOrFail ( id ) ; } catch ( id ) { res . status ( 401 ) . send ( ) ; } if ( roles . indexOf ( user . role ) > - 1 ) next ( ) ; else res . status ( 401 ) . send ( ) ; } ; } ;