Hi there,
For example, suppose there are data like the following, the user’s name is “John Smith” and There are Email and encrypted password.
I have an input form in HTML where the user enters their Email and password, and the password is encrypted and checked to make sure it matches the password in the database.
I know how to make sure an Email exists.
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const crypto = require("crypto");
app.use(bodyParser.urlencoded({extended: true}));
mongoose.connect("/*.....*/");
const usersSchema = {
email: String,
password: String
}
const User = mongoose.model("User", usersSchema);
app.get("/", function(req, res) {
res.sendFile(__dirname + "/example_form.html");
})
app.post("/", function(req, res) {
User.findOne({ email: req.body.email}).then(user => {
if (user) {
//after process
} else {
res.send("Your email is invalid.");
}
})
})
And I also know how to make sure the Password and Email match.
app.post("/", function(req, res) {
User.findOne({ email: req.body.email}).then(user => {
if (user) {
/*
encryption process
*/
var crypted = cipher.update(userStr, 'utf8', 'hex');
crypted += cipher.final('hex');
const constid = req.body.email;
User.findOne({ email: req.body.email, password: crypted}).then(user => {
if(user) {
res.send("Success.");
} else {
res.send("Email or password is wrong.");
}
})
} else {
res.send("Your email is invalid.");
}
})
})
btw for this situation, how can I get a name value like “John Smith” by entered Email and Password value?