Book Image

Node.js 6.x Blueprints

By : Fernando Monteiro
Book Image

Node.js 6.x Blueprints

By: Fernando Monteiro

Overview of this book

Node.js is the most popular framework to create server-side applications today. Be it web, desktop, or mobile, Node.js comes to your rescue to create stunning real-time applications. Node.js 6.x Blueprints will teach you to build these types of projects in an easy-to-understand manner. The key to any Node.js project is a strong foundation on the concepts that will be a part of every project. The book will first teach you the MVC design pattern while developing a Twitter-like application using Express.js. In the next chapters, you will learn to create a website and applications such as streaming, photography, and a store locator using MongoDB, MySQL, and Firebase. Once you’re warmed up, we’ll move on to more complex projects such as a consumer feedback app, a real-time chat app, and a blog using Node.js with frameworks such as loopback.io and socket.io. Finally, we’ll explore front-end build processes, Docker, and continuous delivery. By the end of book, you will be comfortable working with Node.js applications and will know the best tools and frameworks to build highly scalable desktop and cloud applications.
Table of Contents (16 chapters)
Node.js 6.x Blueprints
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Adding config and passport files


As mentioned earlier, let's create a config file:

  1. Inside server/config, create a file called config.js and place the following code in it:

          // Database URL 
          module.exports = { 
              // Connect with MongoDB on local machine 
              'url' : 'mongodb://localhost/mvc-app' 
          }; 
    
  2. Create a new file on server/config and name it passport.js. Add the following content:

          // load passport module 
          var LocalStrategy    = require('passport-local').Strategy; 
          // load up the user model 
          var User = require('../models/users'); 
     
          module.exports = function(passport) { 
              // passport init setup 
              // serialize the user for the session 
              passport.serializeUser(function(user, done) { 
                  done(null, user.id); 
              }); 
              //       deserialize the user 
              passport.deserializeUser(function(id, done) { 
                  User.findById(id, function(err, user) { 
                      done(err, user); 
                  }); 
              }); 
              // using local strategy 
              passport.use('local-login', new LocalStrategy({ 
                  // change default username and password, to email 
                  //and password 
                  usernameField : 'email', 
                  passwordField : 'password', 
                  passReqToCallback : true 
              }, 
              function(req, email, password, done) { 
                  if (email) 
                  // format to lower-case 
                  email = email.toLowerCase(); 
                  // process asynchronous 
                  process.nextTick(function() { 
                      User.findOne({ 'local.email' :  email }, 
                       function(err, user)
                    { 
                      // if errors 
                     if (err) 
                       return done(err); 
                     // check errors and bring the messages 
                     if (!user) 
                       return done(null, false, req.flash('loginMessage',
                       'No user found.')); 
                    if (!user.validPassword(password)) 
                      return done(null, false, req.flash('loginMessage',
                      'Wohh! Wrong password.')); 
                    // everything ok, get user 
                    else 
                      return done(null, user); 
                    }); 
                  }); 
               })); 
              // Signup local strategy 
              passport.use('local-signup', new LocalStrategy({ 
                  // change default username and password, to email and 
                 //  password 
                  usernameField : 'email', 
                  passwordField : 'password', 
                  passReqToCallback : true 
              }, 
              function(req, email, password, done) { 
                  if (email) 
                  // format to lower-case 
                  email = email.toLowerCase(); 
                  // asynchronous 
                  process.nextTick(function() { 
                      // if the user is not already logged in: 
                      if (!req.user) { 
                          User.findOne({ 'local.email' :  email },
                           function(err,user) { 
                      // if errors 
                      if (err) 
                        return done(err); 
                      // check email 
                      if (user) { 
                        return done(null, false, req.flash('signupMessage',
                         'Wohh! the email is already taken.')); 
                      }
                      else { 
                        // create the user 
                          var newUser = new User(); 
                          // Get user name from req.body 
                          newUser.local.name = req.body.name; 
                          newUser.local.email = email; 
                          newUser.local.password =
                           newUser.generateHash(password); 
                          // save data 
                         newUser.save(function(err) { 
                       if (err) 
                         throw err; 
                         return done(null, newUser); 
                        }); 
                       } 
                    }); 
                   } else { 
                     return done(null, req.user); 
                   }         }); 
              })); 
          }; 
    

    Note that in the fourth line, we are importing a file called models; we will create this file using Mongoose.