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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:50:59+00:00 2026-06-15T20:50:59+00:00

i just started implementing redis with node. during an implementation of authentication method i

  • 0

i just started implementing redis with node. during an implementation of authentication method i need to check whether the token exist in redis, if not update the new token in redis and in my mongo db for that i need to write a big callback block and not getting result properly. how can we make the redis get red of callbacks. how can we make it synchronous. sample code is below.

module.exports.authenticate = function(request, response)   {
    var reply = {};

    if(UserSchema)  {
        var UserModel, attributes;

        /** Registering User Model; **/
        mongoose.model('user', UserSchema);
        UserModel = mongoose.model('user');

        attributes = request.params;

        UserModel.findOne(attributes, "_id name email token", function(error, user) {

            if(!error && user)  {
                var token;

                //delete user.password;
                token = user.token;

                /** Checking token exists in redis; **/
                redisClient.get(token, function(error, value)   {
                    if(value === null && error === null)    {

                        /** Creating new token; **/
                        token = require('crypto').createHash('md5').update("" + (new Date()).getTime()).digest("hex");
                        user.token = token;

                        /** Storing new token on redis; **/
                        setTokenOnRedis(token);

                        /** Updating token in the user model; **/
                        UserModel.update({ _id : user._id}, { token : token }, function(error, user)    {
                            if(error !== null && user === null) {
                                deleteTokenOnRedis(token);

                                /** Error message; **/
                                reply = {
                                    error : true,
                                    code : "AUTH#001",
                                    msg : "User authentication failed, Please check user credentials."
                                }
                                response.send(reply);

                            }else if(error === null && user !== null)   {
                                reply = user;
                                response.send(reply);
                            }
                        });
                    }else if(value !== null)    {
                        reply = user;
                        response.send(reply);
                    }else   {
                        /** Error message; **/
                        reply = {
                            error : true,
                            code : "AUTH#001",
                            msg : "User authentication failed, Please check user credentials."
                        };
                        response.send(reply);
                    }
                });
            }else   {
                /** Error message; **/
                reply = {
                    error : true,
                    code : "AUTH#001",
                    msg : "User authentication failed, Please check user credentials."
                }
            }       
        });
    }else   {

        /** Error message; **/
        reply = {
            error : true,
            code : "AUTH#001",
            msg : "User authentication failed, Please check user credentials."
        }

        response.send(reply);
    }
};
  • 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-15T20:51:01+00:00Added an answer on June 15, 2026 at 8:51 pm

    No you won’t be able to make any io calls synchronous including the redis ones. The only synchronous io calls available that I’m aware of are filesystem and console ones.

    However, there are some coding techniques you can use to make async coding a bit more manageable.

    • return early by checking for err first.
    • move repetitive code into a separate function, e.g, creation of error structures.
    • use this async library: https://github.com/caolan/async. In particular, the waterfall function might be handy here.

    I also think that you will need to pass in a callback method these functions as they are async.

    • setTokenOnRedis(token);
    • deleteTokenOnRedis(token);

    I’ve refactored your sample code which hopefully should be less indented and more readable/maintainable. I haven’t used async, I’ll leave that to you.

    Personally, I found the whole node async coding model very frustrating initially but you get used to it. After a while you learn to use various async coding patterns and then it becomes just about tolerable 🙂

    Some links that you might find helpful:

    • error handling in asynchronous node.js calls
    • Node.js Best Practice Exception Handling
    • How to avoid long nesting of asynchronous functions in Node.js

    refactored code:

    module.exports.authenticate = function(request, response){
      authenticate(request, response, function(err, reply){
        if(err){
           reply = authenticationError(err);
        }
        response.send(reply);
      });
    };
    
    var authenticationError = function(internalmsg){
      return {
        internalmsg : internalmsg,
        error : true,
        code : "AUTH#001",
        msg : "User authentication failed, Please check user credentials."
      };
    };
    
    var authenticate = function(request, response, callback)   {
      if(UserSchema)  {
        var UserModel, attributes;
    
        /** Registering User Model; **/
        mongoose.model('user', UserSchema);
        UserModel = mongoose.model('user');
    
        attributes = request.params;
    
        UserModel.findOne(attributes, "_id name email token", function(err, user) {
          if(err || !user){
            return callback(err || "UserModel.findOne, no user");
          }
    
          var token;
    
          //delete user.password;
          token = user.token;
    
          /** Checking token exists in redis; **/
          redisClient.get(token, function(err, value){
            if(err){
              return callback(err);
            }
            if(value){
              return callback(null, value);
            }
    
            /** Creating new token; **/
            token = require('crypto').createHash('md5').update("" + (new Date()).getTime()).digest("hex");
            user.token = token;
    
            /** Storing new token on redis; **/
            setTokenOnRedis(token);
    
            /** Updating token in the user model; **/
            UserModel.update({ _id : user._id}, { token : token }, function(err, user) {
              if(err || !user) {
                deleteTokenOnRedis(token);
                return callback(err || "UserModel.update, no user found");
              }
              callback(null, user);
            });
          });
        });
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started implementing a share functionality but was wondering if its possible to
I have just started implementing ISet 's instead of IList 's in my project
I'm just getting started defining and implementing external javaScript libraries and I'm a little
I have just started implementing signal listeners in a django project. While I understand
I just started working with OpenGL, but I ran into a problem after implementing
I'm just starting to mess with bindings. I've started implementing a preference dialog, binding
I'm writing a standard Cocoa application, and I've just started implementing AppleScript support for
I just have started to learn ASP.NET and implementing simple application with one GridView
I have just started reading on implementing RESTful web services and creating RESTful apis.
I've just started implementing my WPF application with Caliburn Micro framework (it's my first

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.