accessing session variables in redirected file in node.js

I am building a crappy login system as a newbie. I have done this so far:

app.post("/verifyLogin",function(request,response){
var usr=request.body.username;
var pass=request.body.password;

userModel.find({$and:[{username:usr},{password:pass}]},function(err,user){

if(user.length==0)
{
    response.redirect("/?msg=failed");
}
else
{
    request.session.user=user;
    response.redirect("/dashboard");

}

 });

    });

This works fine but after successful login i want to get the user details in the dashboard. I am clueless. Please shed some light.

EDIT

I have the following setup for dashboard in routes:

app.get("/dashboard",function(request,response){
response.sendfile('/lms/site/dashboard.html');
});

If you mean you want to pass the users' details to a template:

app.get('/dashboard', function(req, res) {
  res.render('dashboard', {
    user : req.session.user
  });
});

This assumes a few things though:

  • you have a working templating setup;
  • you have a template called dashboard (with an extension matching your templating setup);
  • you're going to provide some sort of setup to make sure a user is logged in before they can open /dashboard.

EDIT: since you don't want to use templating, you could use AJAX to get the user details from the server into the client:

// server side
app.get('/userdata', function(req, res) {
  // check if a user is logged in
  ...
  // return the user details as JSON
  res.send(req.session.user);
});

// client side (in 'dashboard.html', this assumes is will load jQuery)
$.getJSON('/userdata', function(user) {
  // process user data, insert it into the DOM somewhere...
});

EDIT 2: to check if a user is logged in, you could create a middleware which would check for the existence of req.session.user and redirect to the login page if it's undefined:

var isLoggedIn = function(req, res, next) {
  if (req.session && req.session.user)
    next(); // user logged in, so pass
  else
    res.redirect('/'); // not logged in, redirect to login page
};

You would use the isLoggedIn middleware for all routes that require a user to be logged in:

app.get('/userdata', isLoggedIn, function(req, res) {
  res.send(req.session.user);
});