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

  • Home
  • SEARCH
  • 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 3309088
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:35:57+00:00 2026-05-17T21:35:57+00:00

I have a node.js Express application with a basic user authentication system where the

  • 0

I have a node.js Express application with a basic user authentication system where the session is stored in redis. I’d like to write and re-use a current_user() function that populates a JSON representation of the logged in user (if there is one) in a current_user variable. Here is the code to retrieve the user:

if( req.cookie('fingerprint') ) {
  redis_client.get("users:fingerprint:"+req.cookie("fingerprint"), function(err,id) {
    redis_client.get("users:id:"+id, function(err,user) {
      current_user = JSON.parse(user);
    })
  })
}

if I only wanted to use this once, I could put a res.render call after that current_user line, but I’d like to populate the current_user variable for all controllers and actions.

I’m new to node, so there’s probably a fundamental concept I’m missing here…but basically I need a way to return the current_user variable from above in a synchronous context or achieve the same effect asynchronously. For example,

app.get('/', function(req,res) {
  current_user = current_user();
  res.render('home', {layout: "layout.ejs", locals: {current_user: current_user}})
})

Thanks.

  • 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-17T21:35:57+00:00Added an answer on May 17, 2026 at 9:35 pm

    Express routes accept a third argument: next, this allows you to pass control to the next matching route. You could use this to build up before filters based on routes, like:

    app.get('/admin', function(req, res, next){
      if(authenticated_user()) {
        req.user = get_user();
        next(); 
      }
      else // prompt for login
    });
    
    app.get('/admin/:id', function(req, res){
      // foo
    }
    

    Hope this helps, for more information see Passing Route Control in the official Express guide

    update

    A DRYer way to achieve the same using the oh-so-cool Route Middleware

    function checkAuthentication(req, res, next){
      if(authenticated_user()) {
        req.user = get_user();
        next(); 
      }
      else // prompt for login
    }
    
    app.get('/admin/:id', checkAuthentication, function(req, res){
      // foo
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a cck-node with some node-references like PDF-files, videos and so on. Now
I start to look at Node.js. Also I'm using Express. And I have a
If I have node.child1.child2 , can I use h asattr(node, 'child1.child2') effectively? Will it
EDIT: I also have access to ESXLT functions. I have two node sets of
I have the following HTML node structure: <div id=foo> <div id=bar></div> <div id=baz> <div
I have a base class Node which contains a list of child nodes. Node
I have the concept of NodeType s and Node s. A NodeType is a
I have a WinForms TreeView with one main node and several sub-nodes. How can
I have been looking for a quadtree/quadtree node implementation on the net for ages.
I have a manager process on a node, and several worker processes. The manager

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.