-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
211 lines (189 loc) · 4.94 KB
/
Copy pathapp.js
File metadata and controls
211 lines (189 loc) · 4.94 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
const findOrCreate = require("mongoose-findorcreate");
// initialized express app
const app = express();
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(
bodyParser.urlencoded({
extended: true,
})
);
// sdssf
app.set("trust proxy", 1); // trust first proxy
app.use(
session({
secret: "Our little secret.",
resave: false,
saveUninitialized: false,
})
);
app.use(passport.initialize());
app.use(passport.session());
const password = process.env.DB_PASSWORD;
const mongoUsername = process.env.DB_USER;
mongoose.connect('mongodb+srv://'+ mongoUsername +':'+ password +'@cluster0.dssa4.mongodb.net/mflix?retryWrites=true&w=majority', () => {
console.log("Databse connected!!");
});
const userSchema = new mongoose.Schema({
email: String,
password: String,
googleId: String,
imgUrl: String,
title: String,
description: String,
link1Name: String,
link1Url: String,
link2Name: String,
link2Url: String,
link3Name: String,
link3Url: String,
link4Name: String,
link4Url: String,
link5Name: String,
link5Url: String,
link6Name: String,
link6Url: String,
});
userSchema.plugin(passportLocalMongoose);
userSchema.plugin(findOrCreate);
const User = new mongoose.model("User", userSchema);
passport.use(User.createStrategy());
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user);
});
});
app.get("/", (req, res) => {
res.render("login");
});
app.get("/login", (req, res) => {
res.render("login");
});
app.post("/login", (req, res) => {
const user = new User({
username: req.body.username,
password: req.body.password,
});
req.login(user, (err) => {
if (err) {
// console.log(err);
res.render("login",{errmsg:err});
} else {
passport.authenticate("local")(req, res, () => {
res.redirect("/create");
});
}
});
});
app.get("/register", (req, res) => {
res.render("signup");
});
app.post("/register", (req, res) => {
User.register(
{ username: req.body.username , title: req.body.title},
req.body.password,
(err, user) => {
if (err) {
// console.log(err);
res.render("signup",{errmsg:err});
} else {
passport.authenticate("local")(req, res, () => {
res.redirect("/create");
});
}
}
);
});
app.get("/create", (req, res) => {
User.findById(req.user.id, (err, foundUser) => {
if (err) {
res.render("login");
} else {
if (foundUser) {
// console.log(req.user.id);
res.render("create", {foundUser:foundUser});
}
}
});
});
app.post("/create", (req, res) => {
const imgUrl = req.body.imgUrl;
const title = req.body.title;
const description = req.body.description;
const title1 = req.body.link1Name;
const url1 = req.body.link1;
const title2 = req.body.link2Name;
const url2 = req.body.link2;
const title3 = req.body.link3Name;
const url3 = req.body.link3;
const title4 = req.body.link4Name;
const url4 = req.body.link4;
const title5 = req.body.link5Name;
const url5 = req.body.link5;
const title6 = req.body.link6Name;
const url6 = req.body.link6;
User.findById(req.user.id, (err, foundUser) => {
if (err) {
console.log(err);
} else {
if (foundUser) {
foundUser.title = title;
foundUser.description = description;
foundUser.imgUrl = imgUrl;
foundUser.imgUrl = imgUrl;
foundUser.link1Name = title1;
foundUser.link1Url= url1;
foundUser.link2Name = title2;
foundUser.link2Url= url2;
foundUser.link3Name = title3;
foundUser.link3Url= url3;
foundUser.link4Name = title4;
foundUser.link4Url= url4;
foundUser.link5Name = title5;
foundUser.link5Url= url5;
foundUser.link6Name = title6;
foundUser.link6Url= url6;
foundUser.save(() => {
res.redirect("/view");
});
}
}
});
});
app.get("/view", (req, res) => {
User.findById(req.user.id, (err, foundUser) => {
if (err) {
console.log(err);
} else {
if (foundUser) {
res.render("view", {foundUser:foundUser});
}
}
});
});
app.get("/:organization", (req, res) => {
// console.log(req.params.organization);
User.findOne({ title: req.params.organization }, function (err, foundUser) {
if (!err) {
if (!foundUser) {
res.render("error");
} else {
res.render("viewgen", {foundUser:foundUser});
}
}
});
});
app.listen(process.env.PORT || 3000, function () {
console.log("Server started on port 3000.");
});