-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
136 lines (111 loc) · 2.92 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const express = require("express");
const mongoose = require("mongoose");
const session = require("express-session");
var MongoDBStore = require("connect-mongodb-session")(session);
const flash = require("connect-flash");
const User = require("./models/user");
const Product = require("./models/product")
const app = express();
const MONGO_URI = "mongodb://localhost:27017/User";
var store = new MongoDBStore({
uri: MONGO_URI,
collection: "sessions",
});
// Put this is a middleware directory
const isAuth = (req, res, next) => {
if (req.session.isLogged) {
next();
}
res.status(302).redirect("/login");
};
const setLocale = (req, res, next) => {
res.locals.isLogged = req.session.isLogged || false;
next();
};
app.use(
session({
secret: "keyboard cat",
resave: false,
saveUninitialized: true,
store: store,
})
);
mongoose
.connect(MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
app.listen("3000", () => {
console.log("Listening to port 3000");
});
})
.catch((err) => {
console.log("OH NO error");
});
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: true }));
app.use(express.json({}));
app.use(flash());
app.use(setLocale);
app.get("/", (req, res) => {
res.render("home", { user: req.session.user || "Guest" });
});
app.get("/login", (req, res) => {
res.render("login");
});
app.get("/signup", (req, res) => {
res.render("signup", {
error: req.flash("message"),
});
});
app.post("/signup", async (req, res) => {
const { password, cpassword, firstname, lastname, email } = req.body;
User.findOne({ email: email }).then(async (user) => {
if (user) {
console.log(user);
req.flash("message", "Email already exixts!");
return res.redirect("/signup");
}
if (password != cpassword) {
req.flash("message", "Confirm Password does not match!");
return res.redirect("/signup");
}
const epasswd = password;
const newUser = new User({
firstname,
lastname,
password: epasswd,
email,
});
await newUser.save();
req.session.isLogged = true;
res.redirect("/login");
});
});
app.post("/login", async (req, res) => {
var email = req.body.email;
var password = req.body.password;
const database = await User.findOne({ email: email });
if (!database) {
res.send("NO USER FOUND");
} else if (database.password != password) {
res.send("INCORRECT PASSWORD");
} else {
req.session.isLogged = true;
req.session.user = database;
res.redirect("/");
}
});
app.get("/logout", (req, res) => {
req.session.destroy();
res.redirect("/login");
});
app.get("/mens", async(req, res) => {
const products = await Product.find({gender:'Men'});
res.render("men", { user: req.session.user || "Guest", products: products })
});
app.use((req, res) => {
res.status(404).send("NOT FOUND");
});