简体   繁体   中英

Session variable set in route middleware is not accessible throughout application in nodejs + concurrency issues

I am creating REST api's in nodejs with jsonwebtoken for authentication.Problem is I have created my own route middleware which decodes token and sets req.session as well as middleware object property to the decoded token value but I can only access req.session in router.post() method but cannot access that sessiondata property set in middleware function.

Below is my sample code

user.js

 var router=require('express').Router();
 var AuthenticateLib=require('app/libraries/authentication');

 router.post('/updateprofile',AuthenticateLib.verifyToken,function(req,res){
  console.log(req.session); // is accessible over here
  console.log(AuthenticateLib.sessiondata) // prints null
});

authentication.js

  module.exports={
  sessiondata:null,
  verifyToken:function(req,res,next){
           var token = req.headers.authorization;
            return jwt.verifyAsync(token,config.getParam('security.jwtSecret'))
                    .then(decoded => {
                            req.session=decoded;
                            this.sessiondata=decoded;
                            next(); 
                    })
                    .catch(...)
       }}

I created sessiondata property because req object is not avaliable all over application so I thought may be I could require AuthenticateLib's and access sessiondata property in my files wherever I need to access my session.

My aim is that I should be able to access my session data not only in router.post but in any other file in application.

I tried adding AuthenticateLib.sessiondata=req.session; just after router.post line then everything works fine but is it the only way.Why not sessiondata property set in middleware doesn't work. Also how can I solve concurrency issues related to sessiondata property.

Any help would be appreciated.

To solve the issue of concurrency I did something like

authentication.js

           function fname()
           {
             var x,y,mysessiondata; // all private vars
             return {
                     setsessiondata:function(data){mysessiondata=data;}, // public methods and vars
                     foo:function(){},.....   
             }
           }

           module.exports=fname;

Technically I didn't solve issue of session just modified my design pattern so that sessiondata would be set on every new obj created on each request which would be unique to each request which in turn solves my concurrency and overall session issue.

var AuthenticateLibObj=require('authentication');

 router.post('/updateprofile',AuthenticateLib.verifyToken,function(req,res){ 
          var Obj=new AuthenticateLibObj();
          Obj.setsessiondata(req.session);
          Obj.foo(); // so I dont need to pass session to foo again it can get it from its private datamember `mysessiondata`
          })

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