samples/sample3/handlers/sessions.js
function identifyUser(req, res, next) {
  // Get the session ID from the session cookie
  var sessionId = req.cookies.session;
  req.session = null;

  if (sessionId) {
    // Get the session REST resource
    var resource = new swagger.Resource('/sessions', sessionId, null);
    req.app.dataStore.get(resource, function(err, resource) {
      if (resource) {
        // Add the session to the request, so other middleware can use it
        req.session = resource.data;
        console.log('Current User: %s', req.session.user.username);
      }
      else {
        console.warn('INVALID SESSION ID: ' + sessionId);
      }
      next();
    });
  }
  else {
    console.log('Current User: ANONYMOUS');
    next();
  }
}
function identifyUser(req, res, next) {
  // Get the session ID from the session cookie
  var sessionId = req.cookies.session;
  req.session = null;

  if (sessionId) {
    // Get the session REST resource
    var resource = new swagger.Resource('/sessions', sessionId, null);
    req.app.dataStore.get(resource, function(err, resource) {
      if (resource) {
        // Add the session to the request, so other middleware can use it
        req.session = resource.data;
        console.log('Current User: %s', req.session.user.username);
      }
      else {
        console.warn('INVALID SESSION ID: ' + sessionId);
      }
      next();
    });
  }
  else {
    console.log('Current User: ANONYMOUS');
    next();
  }
}
function identifyUser ( req , res , next ) { // Get the session ID from the session cookie var sessionId = req . cookies . session ; req . session = null ; if ( sessionId ) { // Get the session REST resource var resource = new swagger . Resource ( '/sessions' , sessionId , null ) ; req . app . dataStore . get ( resource , function ( err , resource ) { if ( resource ) { // Add the session to the request, so other middleware can use it req . session = resource . data ; console . log ( 'Current User: %s' , req . session . user . username ) ; } else { console . warn ( 'INVALID SESSION ID: ' + sessionId ) ; } next ( ) ; } ) ; } else { console . log ( 'Current User: ANONYMOUS' ) ; next ( ) ; } }
Identifies the user via the session cookie on the request.
`req.session` is set to the user's session, so it can be used by subsequent middleware.
Identifies the user via the session cookie on the request . req . session is set to the user s session so it can be used by subsequent middleware .