Book Image

Learning Node.js for Mobile Application Development

Book Image

Learning Node.js for Mobile Application Development

Overview of this book

Table of Contents (21 chapters)
Learning Node.js for Mobile Application Development
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
14
Creating an E-Commerce Application Using the Ionic Framework
Index

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 error',
      message: 'Incorrect...