在会话变量中设置数据并在nodejs中检索

时间:2016-03-08 10:28:55

标签: javascript node.js

以下代码

var uuid = require('uuid');
var S = require('string');
app = express();


var sessions = {};


var logon = function(sessionData) {
    var sessionID = uuid.v1();
    sessions[sessionID] = sessionData;

    return sessionID;
};
app.post("/login", function(req, res) { 


    var sessionData = {

        ldap:{
          url: 'ldap://host:389'+'DC=ind,DC=double,DC=com',
          bindDN: 'DC=ind,DC=double,DC=com',
          username: 'MyUsername',
          password: 'MyPassword'

        },
        // Information related to the current user
        uid: req.body.uid,
        passwd: req.body.passwd     

    };


    var adminClient = ldap.createClient({
        url: sessionData.ldap.url
    });


        if (err != null){
            res.send("Error: " + err);
        }
        else
            // Search for a user with the correct UID.
            adminClient.search(sessionData.ldap.bindDN, {
                scope: "sub",
                filter: "(sAMAccountName=" + sessionData.uid + ")"
            }, function(err, ldapResult) {
                if (err != null){
                  throw err;}
                else {

                    ldapResult.on('searchEntry', function(entry) {
                        sessionData.name = entry.object.cn;

                        var userClient = ldap.createClient({
                            url: sessionData.ldap.url
                        });
                        userClient.bind(sessionData.ldap.username, sessionData.ldap.password, function(err) {
                            if (err == null) {
                                var sessionID = logon(sessionData);

                                res.setHeader("Set-Cookie", ["sessionID=" + sessionID]);
                                res.redirect("home.html");
                            } else
                                res.send("You are not " + sessionData.uid);
                        });
                    });

                    /*If we get to the end and there is no DN, it means there is no such user.
                    ldapResult.on("end", function() {
                        if (sessionData.dn === "")
                            res.send("No such user " + sessionData.uid); 
                    });*/
                }

            });
    });
    var name=sessionData.uid.substr(2);


    var options={
      host: 'test.double.com',
      port: 80,
      contentType: "application/json",
      path: '/LOBReport/home/report/assoId/'+name,
      method: 'GET'  
    };


    http.request(options,function(res){
      console.log('STATUS:'+res.statusCode);

      console.log('HEADERS:'+JSON.stringify(res.headers));

      res.setEncoding('utf8');

      var responseString = '';
      var EmpID = req.sessionData.EmpID;



      res.on('data',function(data){

        responseString += data; 
        console.log("responseString: "+responseString);

        var obj = JSON.parse(responseString);
        var keys = Object.keys(obj);
        for (var i = 0; i < keys.length; i++) {
          req.sessionData.EmpID=obj[keys[i]].employeeId;      
             }                                                                                      
      });         


    }).end();


});

app.get("/sessions", function(req, res) {
    res.send(JSON.stringify(sessions)); 
});


app.get("/userData.js", function(req, res) {
    var data = {};

    if (sessions[req.cookies.sessionID] != undefined) {
        data.name = sessions[req.cookies.sessionID].name;
        data.uid = sessions[req.cookies.sessionID].uid;
    }

    res.send("var userData = " + JSON.stringify(data) + ";");
});


var sessionLifetime = 60;   // In minutes
setInterval(function() {

    for(var sessionID in sessions) 
        if (sessions[sessionID].old) 
            delete sessions[sessionID];
        else
            sessions[sessionID].old = true;

}, sessionLifetime * 60 * 1000);

上面是app.js,我有ldap身份验证,然后从restful客户端调用中检索用户数据。我需要在现有会话变量sessionData中添加数据。

0 个答案:

没有答案