/**
 * 403 Forbidden Network Error Interceptor
 *
 * This interceptor redirects to login page when
 * any 403 session expired error is returned in any of the
 * network requests
 */

var LOGIN_ROUTE = '/login';
var SESSION_EXPIRED = 'session_expired';

var subdomainMatch = /https?:\/\/([^.]+)/;

module.exports = function (xhr, textStatus, errorThrown) {
  if (xhr.status !== 403) return;

  var error = xhr.responseJSON && xhr.responseJSON.error;

  if (error === SESSION_EXPIRED) {
    window.location.href = getRedirectURL();
  }
};

function getRedirectURL () {
  // We cannot get accountHost and username from configModel
  // and userModel because of static pages. God save static pages.
  var username = getUsernameFromURL(location.href);

  if (!username) {
    return '';
  }

  var newURL = location.origin.replace(subdomainMatch, function () {
    return username;
  });

  return location.protocol + '//' + newURL + LOGIN_ROUTE;
}

function getUsernameFromURL (url) {
  var usernameMatches = window.location.pathname.split('/');

  if (usernameMatches.length > 2) {
    return usernameMatches[2];
  }

  var subdomainMatches = url.match(subdomainMatch);

  if (subdomainMatches) {
    return subdomainMatches[1];
  }

  return '';
}

/**
 * 403 Forbidden Network Error Interceptor
 *
 * This interceptor redirects to login page when
 * any 403 session expired error is returned in any of the
 * network requests
 */

var LOGIN_ROUTE = '/login?error=session_expired';
var SESSION_EXPIRED = 'session_expired';

var subdomainMatch = /https?:\/\/([^.]+)/;

module.exports = function (xhr, textStatus, errorThrown) {
  if (xhr.status !== 403) return;

  var error = xhr.responseJSON && xhr.responseJSON.error;

  if (error === SESSION_EXPIRED) {
    window.location.href = getRedirectURL();
  }
};

function getRedirectURL () {
  // We cannot get accountHost and username from configModel
  // and userModel because of static pages. God save static pages.
  var username = getUsernameFromURL(location.href);

  if (!username) {
    return '';
  }

  var newURL = location.origin.replace(subdomainMatch, function () {
    return username;
  });

  return location.protocol + '//' + newURL + LOGIN_ROUTE;
}

function getUsernameFromURL (url) {
  var usernameMatches = window.location.pathname.split('/');

  if (usernameMatches.length > 2) {
    return usernameMatches[2];
  }

  var subdomainMatches = url.match(subdomainMatch);

  if (subdomainMatches) {
    return subdomainMatches[1];
  }

  return '';
}

Add error parameter to forbidden interceptor
Add error parameter to forbidden interceptor
