0

I am not sure why I am getting this error:

"Error: Can't set headers after they are sent."

It is simple API built on express.js which will check whether the user is logged in or not and if the user is not logged in, it would just take the user to the login page.

When you look at the code below starting from fetch(authApiUrl, config), if the user is logged in, it would give status of 200. If the user is not logged in, it would give status for 401 and it would go into the "else" statement and initiate redirectToAccountSignin(request, response, wwwS);.

Then it would go into the redirectToAccountSignin function. So far, when I run this code, it does go into redirectToAccountSignin function, but I believe it throws error on response.redirect(wwwS + '/account/signin?method=initialize&TARGET=' + encodedTargetUrl);.

Is there a problem with my "redirect" method? What am I doing wrong? Can anyone please help me with this?

const fetch = require("node-fetch");
const splunkLogFormat = require('./logUtilities');

function authenticate(request, response, next, successCallback, configuration) {
  // get environment for URL call and grab from environment json
  const appName = !!configuration.appName ? configuration.appName : 'micro-server-app';
  const runtimeEnvironment = !!configuration.environment ? configuration.environment : 'dev';

  const logPreamble = splunkLogFormat(appName, 'authenticate');
  const wwwS = !!environments && !!environments[runtimeEnvironment] && environments[runtimeEnvironment].www_s;

  const authApiUrl = wwwS + '/api/auth/login/check';
  const headers = {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    cookie: request.headers.cookie
  };
  const method = 'GET';
  const config = { headers, method };
  fetch(authApiUrl, config)
    .then(authResponse => {
      const status = authResponse.status;
      if (status === 200) {
        successCallback(request, response, next);
      } else {
        redirectToAccountSignin(request, response, wwwS);
      }
    })
    .catch(error => {
      redirectToAccountSignin(request, response, wwwS);
    });
};

function redirectToAccountSignin(request, response, wwwS) {
  const hostname = !!request && request.hostname;
  const protocol = 'https://';
  const url = !!request && request.originalUrl;
  const encodedTargetUrl = encodeURIComponent(protocol + hostname + url);

  response.redirect(wwwS + '/account/signin?method=initialize&TARGET=' + encodedTargetUrl);
  response.end();
};

module.exports = authenticate;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Eunicorn
  • 601
  • 6
  • 16
  • 29

1 Answers1

-1

Are you sure you want to use res.end() after res.redirect()? https://stackoverflow.com/a/54874227/4208845 What writing are you doing before that?

Thomas Cayne
  • 1,132
  • 8
  • 15
  • Putting ```res.end()``` after ```res.redirect``` shouldn't be an issue. I actually tried removing ```res.end()``` but that did not solve my issue. All I am trying to do is redirect the user that ```url``` inside the ```res.redirect()``` when the user reach into that function. – Eunicorn Jan 14 '20 at 01:43