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);
}
};
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.
I also think that you will need to pass in a callback method these functions as they are async.
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:
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);
});
});
});
}
};