I am having the hardest time understanding what is going wrong. I am building an app that takes user email and password then stores it in a data base. problem: the user password is storing but the user email is not making it to where no one can log in because their email is not in the database. I am using mongoose and studio3T. I am posting my code as well.
require('dotenv').config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const encrypt = require("mongoose-encryption");
const app = express();
app.use(express.static("public"));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true
}));
mongoose.connect("mongodb://localhost:27017/userDB", { useNewUrlParser: true });
const userSchema = new mongoose.Schema({
email: String,
password: String
});
userSchema.plugin(encrypt, { secret: process.env.SECRET, encryptedFields: ['password'] });
const User = new mongoose.model("User", userSchema);
app.get("/", function (req, res) {
res.render("home");
});
app.get("/login", function (req, res) {
res.render("login");
});
app.get("/register", function (req, res) {
res.render("register");
});
app.post("/register", function (req, res) {
const newUser = new User({
email: req.body.email,
password: req.body.password
});
newUser.save(function (err) {
if (err) {
console.log(err);
} else {
res.render("secrets");
}
});
});
app.post("/login", function (req, res) {
const username = req.body.username;
const password = req.body.password;
User.findOne({ email: username }, function (err, foundUser) {
if (err) {
console.log(err);
} else {
if (foundUser) {
if (foundUser.password === password) {
res.render("secrets");
}
}
}
});
});
app.listen(3000, function () {
console.log("Server started on port 3000.");
});