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.
Any help would be appreciated.
via Vibhas
No comments:
Post a Comment