Problem

Jamie Munro
Jul 30, 2017 · 2 min read

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.

Problem courtesy of: Neil Sarkar

Solution

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
});

Solution courtesy of: Daniel Mendel

View additional discussion.

Jamie Munro

Written by

Author of 20 Recipes for Programming PhoneGap, 20 Recipes for Programming MVC 3, and Rapid Application Development with CakePHP.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade