Full User Controller Code

Listing 26.9 shows the complete users_controller.js code. Notice that the crypto library is loaded and used to implement the hashPW() function that creates the hashed password values. Also notice that the User schema is loaded to provide access to the database in the route handlers.

Listing 26.9 users_controller.js: Fully implementing the routes that interact with the User model

01 var crypto = require('crypto');02 var mongoose = require('mongoose'),03     User = mongoose.model('User');04 function hashPW(pwd){05   return crypto.createHash('sha256').update(pwd).06          digest('base64').toString();07 }08 exports.signup = function(req, res){09   var user = new User({username:req.body.username}); ...

Get Node.js, MongoDB, and AngularJS Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.