Express Session saved only for one specific route

181
March 31, 2018, at 8:14 PM

I am new at using express-session and node.js, been having this problem ever since I started. I am saving the session in post request and redirecting it to another route with get request. But there I find that the session is not being saved and it is the default session set.

 //session declaration
 app.use(bodyParser.json());
 app.use(bodyParser.urlencoded({extended: true}));
 app.use(cors());
 app.use(session({
     secret: 'keyboard cat'
 }));
 app.post('/check', function(req, res, next){
var uName = req.body.username;
var pwd = req.body.password;
console.log(uName);
if(uName == ' '|| pwd == ' '|| uName == ''|| pwd == '') {
    res.status(401).json({message: "Sign Up failed"});
}
else {
    var checkUser = "select user from users_default where username='"+uName+"' and password='"+pwd+"'";
    console.log('this is thte query' + checkUser);

connection.query(checkUser, function(err, rows, fields){
    if(err) {
        throw err;
    }
    else
        if(rows.length>0) {
            //saving session
            req.session.user = uName;
            //logging session
            console.log(req.session);
            res.redirect('/welcome');
        }
    else {
        res.status(401).json({message: "Login failed"});
    }
})
}
})

app.get('/welcome', function(req, res){
   res.send('If you are seeing this page, it means you are logged in');
   //logging output
   console.log(req.session);
})

Console Log output:

Session {
  cookie:
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true },
  user: 'varun.khatri@sjsu.edu' }
Session {
  cookie:
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true } }

As you can see, the user field only appears for the console log output of post request, any help will be appreciated.

Rent Charter Buses Company
READ ALSO
Apostrophe CMS align custom layout widget

Apostrophe CMS align custom layout widget

I'm sure there's a really simple solution to this but I can't seem to find it, and I haven't found the question asked here already

210
Node Js saving to a txt file not working

Node Js saving to a txt file not working

I have a server running on Node JsWhat I'm doing is whenever the users submit something to save their input to a text file

183
Node.js mysql error handling in promise

Node.js mysql error handling in promise

I'm using nodejs and express, also mysql

1008
postgres failed to create a backend 0 connection

postgres failed to create a backend 0 connection

i'm using knex on nodejswe have some nodejs process (matching) which send a lot of queries to db (select query)

267