all files / express-stormpath/lib/middleware/ login-required.js

87.5% Statements 7/8
75% Branches 3/4
100% Functions 1/1
87.5% Lines 7/8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                                                         
'use strict';
 
/**
 * This callback, when called, will simply continue processing the HTTP
 * request.
 *
 * @callback nextCallback
 */
 
/**
 * Assert that a user is logged into an account before allowing the user to
 * continue.  If the user is not logged in, they will be redirected to the login
 * page.
 *
 * @method
 *
 * @param {Object} req - The http request.
 * @param {Object} res - The http response.
 * @param {nextCallback} next - The callback which is called to continue
 *   processing the request if the user is authenticated.
 */
module.exports = function (req, res, next) {
  var config = req.app.get('stormpathConfig');
 
  if (req.user) {
    return next();
  }
 
  Eif (req.accepts(['html', 'json']) === 'html') {
    var url = config.web.login.uri + '?next=' + encodeURIComponent(req.originalUrl);
    return res.redirect(302, url);
  }
 
  res.status(401).end();
};