all files / express-stormpath/lib/oauth/ error-responder.js

33.33% Statements 9/27
0% Branches 0/10
0% Functions 0/3
33.33% Lines 9/27
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75                                                                                                                                   
'use strict';
 
var _ = require('lodash');
var url = require('url');
 
var render = require('../helpers/render');
var writeJsonError = require('../helpers/write-json-error');
var forms = require('../forms');
var common = require('./common');
 
/**
 * Takes an error object and renders the login
 * form with that error.
 *
 * @method
 *
 * @param {Object} req - The http request.
 * @param {Object} res - The http response.
 * @param {Object} config - Stormpath config.
 * @param {Object} err - The error to display.
 */
function renderLoginFormWithError(req, res, config, err) {
  var logger = req.app.get('stormpathLogger');
  var view = config.web.login.view;
  var nextUri = url.parse(req.query.next || '').path;
  var encodedNextUri = encodeURIComponent(nextUri);
  var formActionUri = config.web.login.uri + (nextUri ? ('?next=' + encodedNextUri) : '');
  var oauthStateToken = common.resolveStateToken(req, res);
 
  var hasSocialProviders = _.some(config.web.social, function (socialProvider) {
    return socialProvider.enabled;
  });
 
  // Stormpath is unable to create or update the account because the
  // Facebook or Google response did not contain the required property.
  if (err.code === 7201) {
    logger.info('Provider login error: ' + err.message);
    err.userMessage = 'Login failed, because we could not retrieve your email address from the provider.  Please ensure that you have granted email permission to our application.';
  }
 
  var options = {
    form: forms.loginForm,
    formActionUri: formActionUri,
    oauthStateToken: oauthStateToken,
    hasSocialProviders: hasSocialProviders,
    error: err.userMessage || err.message
  };
 
  render(req, res, view, options);
}
 
/**
 * Takes an error object and responds either by
 * rendering the login form with the error, or
 * by returning the error as JSON.
 *
 * @method
 *
 * @param {Object} req - The http request.
 * @param {Object} res - The http response.
 * @param {Object} err - The error to handle.
 */
function oauthErrorResponder(req, res, err) {
  var accepts = req.accepts(['html', 'json']);
  var config = req.app.get('stormpathConfig');
 
  if (accepts === 'json') {
    return writeJsonError(res, err);
  }
 
  return renderLoginFormWithError(req, res, config, err);
}
 
module.exports = oauthErrorResponder;