-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (84 loc) · 2.34 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const dotenv = require("dotenv");
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const encrypt = require("mongoose-encryption");
//setup express
const app = express();
dotenv.config();
//Set view engine and language ejs
app.set("view engine", "ejs");
//setup bodyParser and express to use public static folder
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(express.static("public"));
//setup mongoose connection
// mongoose.set("useUnifiedTopology", true);
// mongoose.connect("mongodb://localhost:27017/userDB", { useNewUrlParser: true });
// //setup a model, and schema
// 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("/register", function (req, res) {
res.render("register");
});
app.get("/login", function (req, res) {
res.render("login");
});
app.post("/register", function (req, res) {
//create new user
const newUser = new User({
email: req.body.username,
password: req.body.password,
});
//if no errors, save user, render the secrets page.
newUser.save(function (err) {
if (err) {
res.send(err);
} else {
res.render("secrets");
}
});
});
app.post("/login", function (req, res) {
//save the user and pass to be used as conditional statements
const username = req.body.username;
const password = req.body.password;
//search userDB for any email matching username
User.findOne({ email: username }, function (err, user) {
if (err) {
//IF any errors, log them
console.log(err);
} else {
//IF a user has been found
if (user) {
//IF the User password is correct
if (user.password === password) {
//Render secrets page
res.render("secrets");
} else {
//ELSE the passwords didnt match
console.log("Wrong username or password. Please try again");
res.render("login");
}
}
}
});
});
//Listen for server
app.listen(process.env.PORT || 3000, function () {
console.log("Server started on port 3000");
});