const express = require("express");
const passport = require("passport");
const User = require("../../models/user");
const router = express.Router();
const {ensureAuthenticated} = require("./authenticationMiddleware");

router.use((req, res, next) => {
  res.locals.currentUser = req.user;
  res.locals.errors = req.flash("error");
  res.locals.infos = req.flash("info");
  next();
});

router.get("/", ensureAuthenticated, (req, res) => {
  res.render("admin/index", {username: req.user.username});
});

router.get("/login", (req, res) => {
  if (req.user) {
    res.redirect("/admin");
  } else {
    res.render("admin/login");
  }
});

router.post("/login", passport.authenticate("login", {
  successRedirect: "/admin/",
  failureRedirect: "/admin/login",
  failureFlash: true
}));

router.get("/logout", ensureAuthenticated, (req, res) => {
  req.logout();
  res.redirect("/admin/login");
});

router.get("/signup", (req, res) => {
  res.render("admin/signup");
});

router.post("/signup", (req, res, next) => {

  const username = req.body.username;
  const password = req.body.password;

  User.findOne({username: username}, function (err, user) {

    if (err) { return next(err); }
    if (user) {
      req.flash("error", "User already exists");
      return res.redirect("admin/signup");
    }

    const newUser = new User({
      username: username,
      password: password
    });
    newUser.save(next);

  });
}, passport.authenticate("login", {
  successRedirect: "/admin/login",
  failureRedirect: "/admin/signup",
  failureFlash: true
}));

router.get("/users", ensureAuthenticated, (req, res, next) => {
  User.find()
      .sort({createdAt: "descending"})
      .exec(function (err, users) {
        if (err) { return next(err); }
        res.render("admin/userlist", {users: users});
      });
});

router.get("/users/:username", ensureAuthenticated, (req, res, next) => {
  User.findOne({username: req.params.username}, (err, user) => {
    if (err) { return next(err); }
    if (!user) { return next(404); }
    res.render("admin/profile", {user: user});
  });
});

//router.post("/users/:username/edit", ensureAuthenticated, (req, res, next) => {
//  req.user.displayName = req.body.displayname;
//  req.user.bio = req.body.bio;
//  req.user.save((err) => {
//    if (err) {
//      next(err);
//      return;
//    }
//    req.flash("info", "Profile updated!");
//    res.redirect('/users');
//  });
//});

module.exports = router;

@@ -2083,18 +2083,16 @@
 );%0A%7D);%0A%0A
-//
 router.p
@@ -2163,18 +2163,16 @@
 t) =%3E %7B%0A
-//
   req.us
@@ -2210,18 +2210,16 @@
 ayname;%0A
-//
   req.us
@@ -2241,18 +2241,16 @@
 dy.bio;%0A
-//
   req.us
@@ -2268,18 +2268,16 @@
 r) =%3E %7B%0A
-//
     if (
@@ -2283,18 +2283,16 @@
 (err) %7B%0A
-//
       ne
@@ -2300,18 +2300,16 @@
 t(err);%0A
-//
       re
@@ -2318,18 +2318,14 @@
 rn;%0A
-//
     %7D%0A
-//
     
@@ -2363,18 +2363,16 @@
 ted!%22);%0A
-//
     res.
@@ -2395,18 +2395,14 @@
 ');%0A
-//
   %7D);%0A
-//
 %7D);%0A

edit profile