With my assumption that there is only single session variable per node application, i'm trying to access value set in session object in one route, from the other route
Kindly help me with this issue
app.post(/OTP) one route
function OTPgenerator(req,res,next){
var string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let OTPvalue='';
// Find the length of string
var len = string.length;
for (let i = 0; i < 8; i++ ) {
OTPvalue += string[Math.floor(Math.random() * len)];
}
console.log(OTPvalue,"OTP value");
req.OTP=OTPvalue;
req.session.OTP=OTPvalue;
req.session.save();
console.log(req.session); //this gives me value of session having OTP in it
next();
app.post('/OTP',OTPgenerator,(req,res)=>{
const OTP={
username:req.body.username,
useremail:req.body.useremail,
OTP:req.OTP
}
confirmation_email(OTP)
.then((resp)=>{
console.log("this is from the user info ",resp);
res.send(req.session);
})
.catch((err)=>{
console.log("this is the error ",err);
})
})
another route (/submit)
app.post('/submit',
(req,res)=>{
console.log(req.session,"session") //the session here doesn't contain value of defined before
if(req.body.OTP!=null){
if(req.session.OTP===req.body.OTP){
const data=new userdata({
_id:mongoose.Types.ObjectId(),
username:req.body.username,
userpassword:req.body.userpassword,
userage:req.body.userage,
useremail:req.body.useremail
})
connectiondb()
.then(async(resp)=>{
console.log("Result of the connection",resp);
await data.save()
.then(resp=>{
console.log("this is response",resp);
res.status(200).send("data successfully saved")
})
.catch(err=>{
console.log("data is not saved ",err)
})
})
.catch(err=>{
console.log("thei si the error ",err);
res.sendStatus(500);
})
}
else if(req.body.OTP===''){
console.log("OTP cannot be null");
res.status(422);
}
else{
console.log('OTP is invalid');
res.status(422).send(req.session);
}
}
else{
res.status(500);
}
})
//for signin
This is my session object configuration
const db='mongodb+srv:xxxxx';
const obj={
useNewUrlParser:true,
useUnifiedTopology:true
}
const connection=mongoose.createConnection(db,obj);
const sessionStore=new MongoStore({
mongooseConnection:connection,
collection:'usersessions'
})
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: false,
store: sessionStore,
cookie:{
secure:true
}
}))
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.