-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
279 lines (235 loc) · 6.81 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// require relevant modules
require('dotenv').config()
const express = require('express')
const bodyParser = require('body-parser')
const ejs = require('ejs')
const nodemailer = require('nodemailer')
const mongoose = require('mongoose')
const session = require('express-session')
const passport = require('passport')
const passportLocalMongoose = require('passport-local-mongoose')
const app = express()
// setting up middleware
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(bodyParser.urlencoded({extended: true}))
// setup session
app.use(session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: false
}))
// flash message middleware
app.use(function(req, res, next){
res.locals.message = req.session.message
delete req.session.message
next()
})
// setup passport
app.use(passport.initialize())
app.use(passport.session())
// connect to a mongoDB database
mongoose.connect('mongodb://localhost:27017/raterDB', {useNewUrlParser: true, useUnifiedTopology: true})
mongoose.set('useCreateIndex', true)
mongoose.set('useFindAndModify', false)
// creating a schema for movie ratings
const ratingSchema = new mongoose.Schema({
name: String,
url: String,
rating: {
type: Number,
min: 0,
max: 10
},
comment: String
})
// creating a user schema for the users
const userSchema = new mongoose.Schema({
email: String,
username: String,
password: String,
ratings: [ratingSchema]
})
// plugin passport to the user schema
userSchema.plugin(passportLocalMongoose)
// creating a model for the userSchema
const User = new mongoose.model('User', userSchema)
// strategy for passport
passport.use(User.createStrategy())
passport.serializeUser(User.serializeUser())
passport.deserializeUser(User.deserializeUser())
// for sending email
var transporter = nodemailer.createTransport({
service: 'gmail',
secure: false,
port: 25,
auth: {
user: '[email protected]',
pass: process.env.PASS
},
tls: {
rejectUnauthorized: false
}
});
// render home page
app.get('/', function(req, res){
User.find((err, foundUser)=>{
if(err){
console.log(err)
} else {
if (foundUser) {
res.render('home', {usersrating: foundUser, authenticated: req.isAuthenticated(), req: req})
}
}
})
})
// post method for home page
app.post('/', function(req, res){
var searchedName = req.body.search
User.find(function(err, name){
if(err){
console.log(err)
}
if(name){
res.render('homeSearch', {users: name, authenticated: req.isAuthenticated(), req: req, searchedName: searchedName})
}
}).elemMatch('ratings', { name: searchedName})
})
// render register page
app.get('/register', function(req, res){
res.render('register')
})
// post method for register page
app.post('/register', function(req, res){
User.register({username: req.body.username, email: req.body.userName2}, req.body.password, function(err, user){
if (err){
req.session.message = {
type: 'danger',
intro: 'Error! ',
message: 'Oops please try again!'
}
res.redirect('register')
} else {
passport.authenticate('local')(req, res, function(){
req.session.message = {
type: 'success',
intro: '',
message: 'you are now registered!'
}
res.redirect('ratings')
})
}
})
})
// Login route
app.get('/login', function(req, res){
res.render('login')
})
// post - login with created credentials
app.post('/login', function(req, res){
if (req.body.username == '' || req.body.password == ''){
req.session.message = {
type: 'danger',
intro: 'Empty fields! ',
message: 'Please insert your credentials'
}
}
const user = new User({
username: req.body.username,
password: req.body.password
})
req.login(user, function(err){
if(err){
console.log(err || !user)
req.session.message = {
type: 'danger',
intro: 'Error! ',
message: 'Wrong credentials. Please try again!'
}
res.redirect('login')
} else {
passport.authenticate('local', {successRedirect: '/ratings', failureRedirect: '/login', failureMessage: req.session.message = {
type: 'danger',
intro: 'Error! ',
message: 'Wrong email or password!'
}})(req, res, function(){
res.redirect('ratings')
})
}
})
})
// rendering ratings page
app.get('/ratings', function(req, res){
if(req.isAuthenticated()){
res.render('ratings', {user: req.user})
} else {
res.redirect('login')
}
})
app.post('/ratings', function(req, res){
var newRating = {
name: req.body.movieName,
url: req.body.imageUrl,
rating: req.body.rating,
comment: req.body.comment
}
User.findOne({username: req.user.username}, function(err, foundUser){
if (err){
console.log(err)
} else {
foundUser.ratings.push(newRating)
foundUser.save()
res.redirect('/ratings')
}
})
})
// creating a delete route
app.post('/delete', function(req, res){
var checkedId = req.body.check
// checks the database for the current user and deletes the id of the rating from it
User.findOneAndUpdate(
{username: req.user.username},
{$pull: {'ratings': {_id: checkedId}}},
{new: true},
function(err){
if (err){
console.log(err)
} else {
res.redirect('/ratings')
}
}
)
})
// contact page
app.get('/contact', function(req, res){
res.render('contact', {authenticated: req.isAuthenticated(), req: req})
})
// post emails from contact page
app.post('/contact', function(req, res){
var mailOptions = {
from: req.body.email,
to: '[email protected]',
subject: req.body.subject,
text: req.body.message
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
res.redirect('/success')
}
});
})
app.get('/success', function(req, res){
res.render('success', {authenticated: req.isAuthenticated(), req: req})
})
// logout of app
app.get('/logout', function(req, res){
req.logout()
res.redirect('/')
})
// listen to port 3000
app.listen(3000, function(){
console.log('Server has started on port 3000')
})