import { Request , Response , NextFunction } from 'express' ; import { HttpException } from '../module/global/model/index' ; import _ = require ( 'lodash' ) ; const SEED = require ( '../../config/seed' ) . SEED ; const jwt = require ( 'jsonwebtoken' ) ; export const authGuard = async function ( req , res , next ) { let token = req . qs . token ; if ( ! _ . isNil ( token ) ) { jwt . verify ( token , SEED , ( err , decoded ) => { if ( err ) { return next ( new HttpException ( 401 , 'Invalid Token' ) ) ; } req . user = decoded . user ; let url = req . baseUrl + req . path ; _ . forEach ( req . params , ( v , k ) => { url = url . replace ( '/' + v , '/:' + k ) ; } ) ; next ( ) ; } ) ; } else { return next ( new HttpException ( 401 , 'Token required' ) ) ; } } ;