<commit_before>'use strict';

/**
 * @file
 * Collection of middleware functions
 */
const loginUrl = '/profile/login';

/*
 * @function
 * Middleware which lets you carry on if you're logged in, else it redirects you to the login page
 */
function ensureAuthenticated (req, res, next) {
  if (req.isAuthenticated()) {
    // the user is logged in
    return next();
  }

  // send user to login otherwise
  res.redirect(loginUrl);
}

/*
 * @function
 * Middleware which takes a destination (String) as a parameter
 * If the string is not set, it falls back to original URL.
 * returns a middleware function which redirects you, yo the destination once you're logged in
 */
function redirectWhenLoggedIn (destination) {
  return (req, res, next) => {
    if (req.isAuthenticated()) {
      if (destination) {
        res.redirect(destination);
      }

      return next();
    }

    req.session.returnTo = destination ? destination : req.originalUrl;
    res.redirect(loginUrl);
  };
}

/*
 * @function
 * Middleware which gets your destination from a callback (that has to return a string)
 * callback gets called the request and response
 */
function redirectToCallbackWhenLoggedIn (cb, alwaysRedirect) {
  return (req, res, next) => {
    if (req.isAuthenticated()) {
      if (alwaysRedirect) {
        res.redirect(cb(req, res));
      }

      return next();
    }

    req.session.returnTo = cb(req, res);<commit_msg>us77: fix af middleware<commit_after>'use strict';

/**
 * @file
 * Collection of middleware functions
 */
const loginUrl = '/profile/login';

/*
 * @function
 * Middleware which lets you carry on if you're logged in, else it redirects you to the login page
 */
function ensureAuthenticated (req, res, next) {
  if (req.isAuthenticated()) {
    // the user is logged in
    return next();
  }

  // send user to login otherwise
  res.redirect(loginUrl);
}

/*
 * @function
 * Middleware which takes a destination (String) as a parameter
 * If the string is not set, it falls back to original URL.
 * returns a middleware function which redirects you, yo the destination once you're logged in
 */
function redirectWhenLoggedIn (destination) {
  return (req, res, next) => {
    if (req.isAuthenticated()) {
      if (destination && destination !== req.originalUrl) {
        return res.redirect(destination);
      }

      return next();
    }

    req.session.returnTo = destination ? destination : req.originalUrl;
    res.redirect(loginUrl);
  };
}

/*
 * @function
 * Middleware which gets your destination from a callback (that has to return a string)
 * callback gets called the request and response
 */
function redirectToCallbackWhenLoggedIn (cb, alwaysRedirect) {
  return (req, res, next) => {
    if (req.isAuthenticated()) {
      if (alwaysRedirect) {
        res.redirect(cb(req, res));
      }

      return next();
    }

    req.session.returnTo = cb(req, res);
{'fraction_non_alphanumeric': 0.1119146005509642, 'fraction_numerical': 0.0006887052341597796, 'mean_word_length': 3.1738505747126435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4299560', 'n_tokens_mistral': 842, 'n_tokens_neox': 826, 'n_words': 295}