Set current user in NodeJS with Express

We always want to access currentUser in all the controllers. I have seen many people use the token to fetch the user all the time.

Instead I think it’s better we set the currentUser in the middleware at the time of authentication.

var jwt = require('jsonwebtoken');
var config = require('./../config/config.constants');

module.exports = function (req, res, next) {

  // check header or url parameters or post parameters for token
  var token = req.body.token || req.query.token || req.headers['x-access-token'] || req.headers['authorization'];

  // decode token
  if (token) {

    var hash = config.secret.replace(/^\$2y(.+)$/i, '\$2a$1');
    // verifies secret
    jwt.verify(token, hash, function (err, decoded) {
      if (err) {
        return res.status(403).json({ message: 'Invalid token' });
      } else {
        // if everything is good, save to request for use in other routes
        req.decoded = decoded;
        wpUserModel.getUserInformationById(, function (err, user) {
          req.currentUser = user;
  } else {
    // if there is no token

    return res.status(403).json({
      message: 'Invalid token'

As you can see in the above gist, I have set currentUser after successfully check the authentication token.

So, now we can easily access currentUser in all your controllers like req.currentUser

If you are looking to get an application developed may it be a web application or a mobile application just drop us a message and experts from Third Rock Techkno will get back to you in 4 hours.

Visit Third Rock Techkno to know more about our expertise in AngularJS, NodeJS and IONIC.

· · · ·

Third Rock Techkno is a leading IT services company. We are a top-ranked web, voice and mobile app development company with over 10 years of experience. Client success forms the core of our value system.

We have expertise in the latest technologies including angular, react native, iOs, Android and more. Third Rock Techkno has developed smart, scalable and innovative solutions for clients across a host of industries.

Our team of dedicated developers combine their knowledge and skills to develop and deliver web and mobile apps that boost business and increase output for our clients.

Angular vs React vs Vue: Which Framework To Use In 2020?

Here is a detailed comparison between Angular vs React vs Vue to help you choose the right framework for your next web app development project.

Read More

Most frequently asked questions about Postman

A postman is a popular tool for interacting with HTTP API clients that makes it easy for developers to generate, share, test and document APIs. Postman gives a friendly GUI for creating requests and responses.

Read More

Agile Scrum Methodology: Scrum Lessons I Learned As A Beginner

When I joined Third Rock Techkno [] as a junior developer, I was delighted to know that I would be working in a scrum team for the very first project of my life. I knew for sure that agile scrum methodology is a great opportunity to grow as a developer. Scrum lessons I learned as a beginner here have imbibed in me the skills and attitude that I’d otherwise have learned much later in my career. Ever since my engineering college days, I was obsessed with joining

Read More