Last active
February 24, 2016 03:04
-
-
Save keithics/9d81325bfa164cd57be8 to your computer and use it in GitHub Desktop.
token authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Signup | |
*/ | |
exports.signup = function(req, res) { | |
console.log(req.body); | |
// For security measurement we remove the roles from the req.body object | |
delete req.body.roles; | |
// Init Variables | |
var user = new User(req.body); | |
var message = null; | |
// Add missing user fields | |
user.provider = 'local'; | |
user.displayName = user.firstName + ' ' + user.lastName; | |
user.token = crypto.randomBytes(64).toString('hex'); | |
// Then save the user | |
user.save(function(err) { | |
if (err) { | |
return res.status(400).send({ | |
message: errorHandler.getErrorMessage(err) | |
}); | |
} else { | |
// Remove sensitive data before login | |
user.password = undefined; | |
user.salt = undefined; | |
req.login(user, function(err) { | |
if (err) { | |
res.status(400).send(err); | |
} else { | |
res.json(user); | |
} | |
}); | |
} | |
}); | |
}; | |
/** | |
* token auth :: username/token verification | |
*/ | |
exports.checkAuth = function(req, res) { | |
User.findOne({username:req.body.username,token:req.body.token},function(err,user){ | |
if(user){ | |
return res.jsonp({message: 'ok'}); | |
}else if(!user && !err) { | |
return res.status(400).send({ | |
message: 'Error Authentication' | |
}); | |
}else{ | |
res.status(400).send(err); | |
} | |
}); | |
}; | |
exports.checkAuthMiddleWare = function(req, res,next) { | |
console.log(req.body); | |
User.findOne({username:req.body.username,token:req.body.token},function(err,user){ | |
if(user){ | |
next(); | |
}else if(!user && !err) { | |
return res.status(400).send({ | |
message: 'Error Authentication' | |
}); | |
}else{ | |
res.status(400).send(err); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment