all files / express-stormpath/lib/helpers/ login-responder.js

100% Statements 9/9
100% Branches 6/6
100% Functions 1/1
100% Lines 9/9
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                        23× 23× 23×   23×     18×  
'use strict';
 
var url = require('url');
var strippedAccountResponse = require('./stripped-account-response');
 
/**
 * If the request has the Accept header set to json, it will respond by just ending the response.
 * Else it will redirect to the configured url.
 *
 * @method
 *
 * @param {Object} req - The http request.
 * @param {Object} res - The http response.
 */
module.exports = function (req, res) {
  var config = req.app.get('stormpathConfig');
  var accepts = req.accepts(['html', 'json']);
  var nextUrl = url.parse(req.query.next || '').path || config.web.login.nextUri;
 
  if (accepts === 'json') {
    return strippedAccountResponse(req.user, res);
  }
 
  res.redirect(302, nextUrl);
};