Adding an authentication module

To maintain modularity and simplify the authentication process, we will create a separate module to validate the access privileges of a given user.

In your project directory, add the following file named authentication.js. Open the file and insert the following:

var db = require('./database'); module.exports = { database: 'OrderBase', collection: 'AccessTokens', generateToken: function (user, callback) { var token = { userID: user._id } } // Persist and return the token db.insert(this.database, this.collection, token, function (err, res) { if (err) { callback(err, null); } else { callback(null, res); } }); }, authenticate: function (user, password, callback) { if (user.password ==== password) { // Create a new token ...

Get Learning Node.js for Mobile Application 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.