Skip to content

Instantly share code, notes, and snippets.

@skysign
Last active July 12, 2024 01:15
Show Gist options
  • Save skysign/bf783d5df1b62260c5e0db0f3d119f6c to your computer and use it in GitHub Desktop.
Save skysign/bf783d5df1b62260c5e0db0f3d119f6c to your computer and use it in GitHub Desktop.
const router = express.Router()
async function myValidationResult(req: Request, res: Response, next: NextFunction) {
const result = validationResult(req)
if (!result.isEmpty()) {
return res.status(400).json({ errors: result.array() })
}
return next();
}
function validationRules(): any[] {
return [
query('api_key', 'api_key is invalid.') <- Adding parameter here, to make api_key like github action secret
.custom(async (api_key) => {
const userldapDN = getLDAPDNfromAPIkey(api_key) <- This is our own logic for authentication
const b = await IsValidAPIKey(api_key, userldapDN)
return new Promise((resolve, reject) => {
if (b) {
resolve(b)
}
else {
reject()
}
})
})
]
}
router.get('/example-endpoint', validationRules(), myValidationResult, doExampleEndpoint)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment