all files / express-stormpath/lib/helpers/ authenticate.js

80.95% Statements 34/42
80% Branches 20/25
100% Functions 7/7
80.95% Lines 34/42
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 76 77 78 79 80 81 82 83                          32× 32× 32×   30× 30× 30×   30×     29×   29× 29×         29× 29×         29× 29×         29× 29×   29×               25×           32×           29×  
'use strict';
 
var stormpath = require('stormpath');
var expandAccount = require('./expand-account');
var createSession = require('./create-session');
 
/**
 * Authenticate a user with username/password credentials.
 *
 * @function
 *
 * @param {Object} options - Authentication options.
 * @param {Object} req - HTTP request.
 * @param {Object} res - HTTP response.
 * @param {function} callback - Function to call when completed.
 */
module.exports = function authenticate(options, req, res, callback) {
  var config = req.app.get('stormpathConfig');
  var logger = req.app.get('stormpathLogger');
  var application = req.app.get('stormpathApplication');
 
  function continueWithAuthentication() {
    options = options || {};
    options.username = options.login || options.username || '';
    options.password = options.password || '';
 
    if (!options.username || !options.password) {
      return callback(new Error('Invalid username or password.'));
    }
 
    var authenticator = new stormpath.OAuthPasswordGrantRequestAuthenticator(application);
 
    authenticator.authenticate(options, function (err, authResult) {
      Iif (err) {
        logger.info('Error when trying to authenticate user.');
        return callback(err);
      }
 
      authResult.getAccount(function (err, account) {
        Iif (err) {
          logger.info('Error when trying to retrieve the account for the authenticated user.');
          return callback(err);
        }
 
        expandAccount(account, config.expand, logger, function (err, expandedAccount) {
          Iif (err) {
            logger.info('Error when trying to expand the account of the authenticated user.');
            return callback(err);
          }
 
          req.user = expandedAccount;
          createSession(authResult, expandedAccount, req, res);
 
          if (config.postLoginHandler) {
            return config.postLoginHandler(expandedAccount, req, res, function (err) {
              Iif (err) {
                logger.info('Error when trying to execute the postLoginHandler after authenticating the user.');
                return callback(err);
              }
 
              callback(null, expandedAccount, authResult);
            });
          }
 
          callback(null, expandedAccount, authResult);
        });
      });
    });
  }
 
  if (config.preLoginHandler) {
    return config.preLoginHandler(options, req, res, function (err) {
      if (err) {
        logger.info('Error when trying to execute the preLoginHandler before authenticating the user.');
        return callback(err);
      }
 
      continueWithAuthentication();
    });
  }
 
  continueWithAuthentication();
};