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 for the user this.generateToken(user, function (err, res) { callback(null, res); });}); } else { callback({ error: 'Authentication...