text: // -------- Create an account via API -------- \\
  // Contains all endpoints related to logging in
  
  // Import User model from server/models/User.js 
  const User = require('../../models/User');
  // Import UserSession model object 
  const UserSession = require('../../models/UserSession');
  
  // Steps in this endpoint:
  //	1) get email and password from request body
  //	2) validate both are proper values 
  //	3) check for existing user with that email
  //	4) create new User object and save
  
  
  module.exports = (app) => {
    /*
     * Sign up
     */
    app.post('/api/account/signup', (req, res, next) => {
      const { body } = req;
      const {
        password
      } = body;
      let {
        email
      } = body;
      
      if (!email) {
        return res.send({
          success: false,
          message: 'Error: Email cannot be blank.'
        });
      }
      if (!password) {
        return res.send({
          success: false,
          message: 'Error: Password cannot be blank.'
        });
      }
  	
      email = email.toLowerCase();
      email = email.trim();
  	
      // Steps:
      // 1. Verify email doesn't exist
      // 2. Save
      User.find({
        email: email
      }, (err, previousUsers) => {
        if (err) {
          return res.send({
            success: false,
            message: 'Error: Server error'
          });
        } else if (previousUsers.length > 0) {
          return res.send({
            success: false,
            message: 'Error: Account already exist.'
          });
        }
  	  
        // Save the new user
        const newUser = new User();
  	  
        newUser.email = email;
        newUser.password = newUser.generateHash(password);
        newUser.save((err, user) => {
          if (err) {
            return res.send({
              success: false,
              message: 'Error: Server error'
            });
          }
          return res.send({
            success: true,
            message: 'Signed up'
          });
        });
      });
    }); // end of sign up endpoint
    
    // get body content: password and email 
    // make sure they're not empty 
    // set email to lower case
    // validate email exits, if not say wrong credentials 
    // using result from email look up:
  		// encrypt entered password and compare with stored one
  		// If SAME: create new user session, save session, 
  			// return doc id from new User Session object 
    app.post('/api/account/signin', (req, res, next) => {
      const { body } = req;
      const {
        password
      } = body;
      let {
        email
      } = body;
      if (!email) {
        return res.send({
          success: false,
          message: 'Error: Email cannot be blank.'
        });
      }
      if (!password) {
        return res.send({
          success: false,
          message: 'Error: Password cannot be blank.'
        });
      }
      email = email.toLowerCase();
      email = email.trim();
      User.find({
        email: email
      }, (err, users) => {
        if (err) {
          console.log('err 2:', err);
          return res.send({
            success: false,
            message: 'Error: server error'
          });
        }
        if (users.length != 1) {
          return res.send({
            success: false,
            message: 'Error: Invalid'
          });
        }
        const user = users[0];
        if (!user.validPassword(password)) {
          return res.send({
            success: false,
            message: 'Error: Invalid'
          });
        }
        // Otherwise correct user
        const userSession = new UserSession();
        userSession.userId = user._id;
        userSession.save((err, doc) => {
          if (err) {
            console.log(err);
            return res.send({
              success: false,
              message: 'Error: server error'
            });
          }
          return res.send({
            success: true,
            message: 'Valid sign in',
            token: doc._id
          });
        });
      });
    }); // end of sign in endpoint
    
    
    // VERIFY: Used to validate user's session token 
    // every time an action is performed 
    app.get('/api/account/verify', (req, res, next) => {
      // Get the token
      const { query } = req;
      const { token } = query;
      // ?token=test
      // Verify the token is one of a kind and it's not deleted.
      UserSession.find({
        _id: token,
        isDeleted: false
      }, (err, sessions) => {
        if (err) {
          console.log(err);
          return res.send({
            success: false,
            message: 'Error: Server error'
          });
        }
        if (sessions.length != 1) {
          return res.send({
            success: false,
            message: 'Error: Invalid'
          });
        } else {
          // DO ACTION
          return res.send({
            success: true,
            message: 'Good'
          });
        }
      });
    }); // end of verify endpoint
    
    
    // LOGGING OUT: Change User Session to delete 
    // API accepts session token and updates the document to deleted 
    app.get('/api/account/logout', (req, res, next) => {
      // Get the token
      const { query } = req;
      const { token } = query;
      // ?token=test
      // Verify the token is one of a kind and it's not deleted.
      UserSession.findOneAndUpdate({
        _id: token,
        isDeleted: false
      }, {
        $set: {
          isDeleted:true
        }
      }, null, (err, sessions) => {
        if (err) {
          console.log(err);
          return res.send({
            success: false,
            message: 'Error: Server error'
          });
        }
        return res.send({
          success: true,
          message: 'Good'
        });
      });
    }); // end of logout endpoint
  };
  