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 6899089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:22:41+00:00 2026-05-27T07:22:41+00:00

I have created a function that restricts route access by verifying that a stored

  • 0

I have created a function that restricts route access by verifying that a stored session user/pass matches what is in the database

var checkAuth = function(req, res, next){
  if(typeof(req.session.user) === 'undefined') {
    req.session.user = { name: '', pass: '', loggedIn: false }
  }
  $R.user.validateLogin(req.session.user, function(err){
    if(err) res.redirect('/login')
    else {
      req.session.user.loggedIn = true
      next()
    }
  })
}

app.get('/restricted', checkAuth, function(req, response){
  response.render('index')
})

It seems to work fine as it will redirect to the /login page if a person is not athenticated, but immediately after redirecting the app shuts down with the error

Error: Can’t set headers after they are sent.

I have traced the error down to the res.redirect(‘/login’) but can’t figure out how to remedy my error.

EDIT: My login route handler

app.get('/login', function(req, response){
  $R.page.addStyles(['forms','user/user'])
  response.render('user/login')
})
app.post('/login', function(req, response){
  $R.user.validateLogin(req.body, function(err, res){
    if(err) response.end(JSON.stringify({error: err.message}))
    else {
      req.session.user = req.body
      response.end(JSON.stringify({ok: true}))
    }
  })
})
  • 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-05-27T07:22:41+00:00Added an answer on May 27, 2026 at 7:22 am

    Your problem is that the function:

    $R.user.validateLogin(req.session.user, function(err){
    

    is aysnchronous. The checkAuth function should return a true/false immediately, or redirect. The current flow of your login is like this:

    1. app.get(‘/restricted’) fires
    2. checkAuth fires
    3. $R.user.validateLogin fires (asynchronously)
    4. At this point, checkAuth returns control to app.get(‘/restricted’)
    5. response.render(‘index’) executes
    6. Code inside of $R.user.validateLogin executes, calling the redirect.

    The problem is you don’t control whether 5 or 6 executes first. Ultimately, both will execute because you aren’t stopping #5 from happening.

    To fix this, your checkAuth function needs to return and/or redirect without using a callback inside (or executing a callback synchronously). Since you are already validating user logins in your ‘login’ route, you should be able to check the user session and return or do the redirect synchronously, like this:

    var checkAuth = function(req, res, next){
      if(typeof(req.session.user) === 'undefined') {
        req.session.user = { name: '', pass: '', loggedIn: false }
      }
    
      if (!req.session.user.loggedIn) {
        // req.session.user.loggedIn = true should be set in the 'login' route, in $R.user.validateLogin
        res.redirect('/login');
      } else {
        // if we already have a req.session.user and they are logged in, keep going
        next();
      }
    }
    

    Apologies for any syntax errors, I didn’t test the above code.

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

Sidebar

Related Questions

I have created a function that updates some values from a database that I
I have created a function that takes a SQL command and produces output that
I have created a function in PHP that calls a webservice and parses through
I have created a function that has as input a char szMyChar; (using it
I have created a function that has the follwing parameter: List<Expression<Func<CatalogProduct, bool>>> orderBy =
I have a function which restricts access to some of the places on my
I have created a function that loops through a set of returned JSON. I
I have (with assistance) created a function that plots and draws a line of
I have (with assistance) created a function that plots and draws a line of
I am novice PHP programmer and I have created a function that changes an

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.