I am trying to make an OAuth authentication flow myself in nodejs, express, and mongodb. I am trying to check if the client is available in my MongoDB before each route. So I made a middleware function that checks if client id and client secret are available in my DB. Here is my middleware function
module.exports = function() {
return function(req, res, next) {
// Implement the middleware function based on the options object
winston.info("check client")
User.getClient(req.get("clientId"), req.get("clientSecret"), function (err, client) {
winston.info("checking time");
if (client) {
return next();
} else {
}
});
winston.info("check client done")
}
}
But as i am calling my mongoose model i think i middleware isnt working perfectly. By the time async call finishes. script is passing through. So what i want to achieve here is that the middleware should pass if the client is available in our db. So middleware has to go forward after it gets response from the mongoose. Here is my router code.
var express = require('express')
, router = express.Router()
, clientVerify = require("../middlewares/clientVerify");
router.use(clientVerify())