text: const User = require("../models/user");
  
  module.exports.renderRegister = (req, res) => {
    res.render("users/register");
  };
  
  module.exports.register = async (req, res, next) => {
    try {
      const { username, email, password } = req.body;
      const user = new User({ username, email });
      const registeredUser = await User.register(user, password);
      // use `login` fn in passport ensure the user is still login after successful registration
      req.login(registeredUser, (err) => {
        if (err) return next(err);
        req.flash("success", "Welcome to Where-To-Visit");
        res.redirect("/locations");
      });
    } catch (e) {
      req.flash("error", e.message);
      res.redirect("register");
    }
  };
  
  module.exports.renderLogin = (req, res) => {
    res.render("users/login");
  };
  
  module.exports.login = (req, res) => {
    req.flash("success", "Glad to See You Again!");
    const redirectUrl = req.session.returnTo || "/locations";
    delete req.session.returnTo;
    res.redirect(redirectUrl);
  };
  
  module.exports.logout = (req, res) => {
    req.logout();
    req.flash("success", "Bye for now!");
    res.redirect("/locations");
  };
  