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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:59:54+00:00 2026-06-13T10:59:54+00:00

I am implementing a restify middleware for session authentication. The function contains nested asynchronous

  • 0

I am implementing a restify middleware for session authentication. The function contains nested asynchronous db calls: db.sessions.remove() within the callback of db.sessions.findOne().

The ‘return’ statements are confusing me because I’m not sure if I am returning next() or next(err) from the callback back to verifyUserSession(), or is it just returning from verifyUserSessions? Am I doing this right?

function verifyUserSession(req, res, next) {


if (req.headers.sessionKey) 
{
    db.sessions.findOne(req.headers.sessionKey, function(err, session) {

        if (err) 
        {
            return next(err);
        }
        if (!session) 
        {
            return next(new Error({'message': 'Session does not exist'}));
        }

        if ((new Date().getTime() - session.timestamp.getTime())/86400000 > 60)
        {
            db.sessions.remove({sessionKey: req.headers.sessionKey}, function(err){
                if (err) 
                {
                    return next(err);
                }
                return next(new Error({'message': 'Session expired'}));
            });
        }
        else
        {
            // session ok
        }
    });
}
}
  • 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-13T10:59:55+00:00Added an answer on June 13, 2026 at 10:59 am

    You are using the callbacks just right.

    return is used only to return from the current callback. When you call return next(...) you invoke the callback function and return the value it returns. Returns are often needed only to make sure you don’t invoke a callback twice.

    Note that you need to make sure every possible branch in your function will invoke the callback, otherwise your program will never proceed from verifyUserSession. In the code example you gave this happens two cases: 1) if session is ok, 2) if req.headers.sessionKey is not set. You should add callbacks for these branches as well.

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

Sidebar

Related Questions

when implementing a recursive function during development, i will use a counter and exit
When implementing a hash table using a good hash function (one where the probability
Before implementing j_security_check using MySQL realm authentication in my web app. I had the
Implementing nested subcommands in Python with cmdln. I'm not sure I'm using the right
Implementing a datastructure with two function pointers, key_eq and key_hash . When these function
Implementing the SQL Server Session Provider by using aspnet_regsql. In Web Config <sessionState mode
Implementing a custom Dependency Property on a Framework Element object causes my Visual Studio
Implementing the ScriptControlClass was extremely easy, unfortunately the side effects with the language implementation
implementing publishActivity in PHP using the REST API using this code: $activity = array(
Implementing a custom membership provider, there are certain properties such as MinRequiredPasswordLength that only

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.