简体   繁体   中英

Async call inside node.js middleware?

I need to do authentication by looking at a session inside a redis database and checking if a value exists, that requires an async call, when I pass the return type inside the async function I get undefined return.

req.isAuthenticated = function() {

  var cookies;
  if(this.headers.cookie) {
    var secrets = require('./config/secrets');
    cookies = cookie.parse(this.headers.cookie);
  }

  // TODO: Maybe figure out how to use this once
  var redisSessionClient = redis.createClient({host:'redis://127.0.0.1/0'});

  // if PHP generated a cookie
  if(cookies['PHPSESSID']) {

    // get the client PHP cookie id
    var sid = cookieParser.signedCookie(cookies['PHPSESSID'], secrets.sessionSecret);

    // get session information inside redis database
    redisSessionClient.get('my_session:'+sid, function(err, session, callback) {

      if(err)
        console.log("error : "+err);
        return err;

      var serializedSession = PHPUnserialize.unserializeSession(session);
      if(serializedSession._sf2_attributes._security_main) {
        console.log('should return true');
        return true; // I want to return here!
      } else {
        console.log('should return false');
        return false; // I want to return here!
      }

    });

    console.log('redisSessionClient is async cant return inside that block');
    // THIS IS BAD, BUT A TEMP HACK, NEED TO CHECK REDIS FIRST BUT ITS ASYNC
    // THIS MEANS AS LONG AS THERE IS A COOKIE WITH THE NAME PHPSESSID THE USER
    // CAN ACCESS LOGGED IN STUFF, THE CONDITION ABOVE READS THE SESSION FROM
    // THE REDIS SESSION STORE AND MAKE SURE THE COOKIE HAS A PARTICULAR VALUE
    return false;

  } else {
    console.log('should return false')
    return false;
  }
};

UPDATED:

req.isAuthenticated = function() {

  var cookies, self = this;
  if(this.headers.cookie) {
    var secrets = require('./config/secrets');
    cookies = cookie.parse(this.headers.cookie);
  }

  // TODO: Maybe figure out how to use this once
  var redisSessionClient = redis.createClient({host:'redis://127.0.0.1/0'});

  // if PHP generated a cookie
  if(cookies['PHPSESSID']) {

    // get the client PHP cookie id
    var sid = cookieParser.signedCookie(cookies['PHPSESSID'], secrets.sessionSecret);

    // get session information inside redis database
    function doCall(callback) {
      redisSessionClient.get('my_Sess:'+sid, function(err, session) {

        if(err) {
          console.log("error : "+err);
          return;
        }

        var serializedSession = PHPUnserialize.unserializeSession(session);
        if(serializedSession._sf2_attributes._security_main) {
          console.log('should return true');
          return callback(true);
        } else {
          console.log('should return false');
          return callback(false);
        }

      });
    }

    doCall(function(response){
      return response;
    });


  } else {
    console.log('should return false')
    return false;
  }
};

Here is the full solution https://jsfiddle.net/Lytsp8e2/1/

req.isAuthenticated = function(cb) {
  // ..........
  if(serializedSession._sf2_attributes._security_main) {
    console.log('should return true');
    cb(true); // I want to return here!
  } else {
    console.log('should return false');
    cb(false); // I want to return here!
  }

Also

if(err)
    console.log("error : "+err);
    return err;

should be

if(err) {
    console.log("error : "+err);
    return err;
}

Here is the full solution https://jsfiddle.net/Lytsp8e2/2/ preserving most of your coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM