O'Reilly logo

Learning Node.js for Mobile Application Development by Milorad Jevdjenic, Stefan Buttigieg

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required