Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8150043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:58:20+00:00 2026-06-06T14:58:20+00:00

I am writing an authentication middleware and it is supposed to redirect to previously

  • 0

I am writing an authentication middleware and it is supposed to redirect to previously visited page after successful login our logout. Here’s the complete config block:

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.static(__dirname + '/public'));
  app.use(express.bodyParser());
  app.use(express.cookieParser());
  app.use(express.session({ secret: '__YouDontKnow__', store: new RedisStore }));
  app.use(authCheck.run);
  app.use(express.methodOverride());
  app.use(app.router);
});

Now when app.use(authCheck.run) is under app.use(express.session), Express throws error: Error: Can’t set headers after they are sent.

If I move this call above app.use(express.session), it throws a 500 error saying that session is undefined. And my auth function depends on the session object.

What can I do?

EDIT: here’s the authCheck function:

exports.run = function (req, res, next) {
  var urlparser = GLOBAL.urlparser,
      url = req.urlp = urlparser.parse(req.url, true),

      goToLastPage = function() {
        var redirectUrl = (req.session.lastVisitedPage) ? req.session.lastVisitedPage : '/';
        console.log("goToLastPage(%s)", redirectUrl);
        res.redirect(redirectUrl);
        return;
      }

  // Log out
  if (url.pathname == '/user/logout') {
    req.session.destroy();
    console.log('User has logged out');
    goToLastPage();
  }

  // Log in
  if (url.pathname == '/user/login' && req.method == 'POST') {
    var email = req.body.login.email,
        password = req.body.login.password;

    req.session.auth = true;
    req.session.userId = 1;

    console.log('User has logged in');

    goToLastPage();
  }

  next();
  return;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:58:22+00:00Added an answer on June 6, 2026 at 2:58 pm

    Put a return after you call goToLastPage(). You’re still calling next() after you have called res.redirect which calls the next piece of middleware in the chain, likely eventually ending up hitting one of your routes, which then tries to set a header and fails.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am busy writing a login page in Silverlight. I am using an Authentication
Here's my situation... I'm writing a .Net/C# security system (authorization and authentication) for a
I am writing a functional test for a page that requires user authentication. I
I'm writing a database authentication system for my web application which is wrriten in
I'm writing a WCF service with some authentication and a custom error handler. However,
I'm writing a web based application that will have its own authorization/authentication mechanism (traditional
I'm writing a c# application that requires user authentication. When the user hits the
I'm a bit lost concerning WCF Server authentication... I'm writing a WCF service that
I'm writing a comprehensive authentication system for an application and I was planning on
I'm writing Qt client for ASP.NET web service with FORMS based authentication. The service

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.