{"size": 2337, "ext": "js", "max_stars_repo_path": "controllers/indexController.js", "max_stars_repo_name": "javitocor/Members-Only-Express", "max_stars_repo_stars_event_min_datetime": "2020-10-14T11:26:46.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-17T04:23:45.000Z", "max_issues_repo_path": "controllers/indexController.js", "max_issues_repo_name": "javitocor/Members-Only-Express", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "controllers/indexController.js", "max_forks_repo_name": "javitocor/Members-Only-Express", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.16, "max_line_length": 86, "alphanum_fraction": 0.6033376123}
var User = require('../models/user');
var Message = require('../models/message');
const bcrypt = require('bcryptjs');
const passport = require("passport");
const { body, validationResult } = require('express-validator');

var async = require('async');


exports.index = function(req, res, next) {
  Message.find({}, 'title text owner')
    .populate('owner')
    .sort([['timestamp', 'descending']])
    .exec(function (err, list_messages) {
      if (err) { return next(err); }
      //Successful, so render
      res.render('index', { title: 'MessagesBoard', list_messages: list_messages });
    });
};

exports.signup_get = (req, res) => res.render("sign-up-form", {title:"Sign Up"});

exports.signup_post = [
  // Validate and santise the name field.
  body('fname', 'First name required').isLength({ min: 3 }).trim().escape(),
  body('lname', 'Last Name required').isLength({ min: 2 }).trim().escape(),
  body('password1', 'Password required').isLength({ min: 5 }).trim().escape(),
  body('username', 'Username required').isLength({ min: 3 }).trim().escape(),
  body('passwordConfirmation').custom((value, { req }) => {
    if (value !== req.body.password1) {
      throw new Error('Password confirmation does not match password');
    }else {
      return true;
    };
  }),
  (req, res, next) => {
    // Extract the validation errors from a request.
    const errors = validationResult(req);
    const user = new User({
      fname: req.body.fname,
      lname: req.body.lname,
      username: req.body.username,
      password: req.body.password1
    });
    if (!errors.isEmpty()) {
      // There are errors. Render the form again with sanitized values/error messages.
      res.render('sign-up-form', { title: 'Sign Up', errors: errors.array()});
      return;
    } else {
      bcrypt.hash(user.password, 10, (err, hashedPassword) => {
        if(err) throw err;
        user.password = hashedPassword;
        user.save(err => {
          if (err) { 
            return next(err);
          };
          res.redirect("/log-in");
        });
      });
    }      
  }
];

exports.login_get = (req, res) => res.render("log-in-form");

exports.login_post = passport.authenticate("local", {
  successRedirect: "/",
  failureRedirect: "/log-in"
});

exports.logout_get = (req, res) => {
  req.logout();
  res.redirect("/");
};
