-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
197 lines (169 loc) · 5.13 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
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const passport = require('passport');
const session = require('express-session');
const flash = require('connect-flash');
//Creating an express app
const app = express();
//set view engine
app.set('view engine', 'ejs');
//static file middleware
app.use(express.static('public'));
// Body-parser middleware
app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded({ extended: false })); // to support URL-encoded bodies
//Express Session
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
//connect-flash middleware
app.use(flash());
//Global Variables
app.use((req, res, next) => {
res.locals.success_message = req.flash('success_message');
res.locals.failure_message = req.flash('failure_message');
res.locals.error = req.flash('error');
next();
});
//Connecting to database
mongoose.connect(process.env.DB_HOST, { useNewUrlParser: true, useUnifiedTopology: true }, (err) => {
if (!err) {
console.log('Connected to database');
}
});
//Model
const User = require('./models/user');
//Passport config local strategise
require('./config/passport');
//Authentication
const auth = require('./config/authentication');
//Routings
app.get('/home', auth.isLogout, (req, res) => {
res.render('home');
});
app.get('/register', auth.isLogout, (req, res) => {
res.render('register');
});
app.get('/login', auth.isLogout, (req, res) => {
const message=req.flash('error');
res.render('login',{message:message});
});
app.get('/notes', auth.isLogin, (req, res) => {
res.render('notes', { user: req.user });
});
app.post('/register', (req, res) => {
// console.log(req.body);
User.findOne({ email: req.body.email }, (err, foundUser) => {
if (err) throw err;
if (foundUser) {
req.flash('failure_message', 'User already Registered!');
// console.log('User already Registered!');
res.redirect('/register');
}
else {
bcrypt.genSalt(10, function (err, salt) {
bcrypt.hash(req.body.password, salt, function (err, hashPassword) {
const user = new User({
name: req.body.name,
email: req.body.email,
password: hashPassword
});
user.save((err) => {
if (err) throw err;
// console.log('User is registered..');
req.flash('success_message', 'You are Successfully Registered.. Please Login To Continue.');
res.redirect('/login');
});
});
});
}
});
});
app.post('/login', passport.authenticate('local', {
successRedirect: '/notes',
failureRedirect: '/login',
failureFlash: true
})
);
app.post('/insert', (req, res) => {
// console.log(req.body);
// console.log(req.user._id);
User.findById(req.user._id, (err, foundUser) => {
if (err) throw err;
if (foundUser) {
foundUser.notes.push({
title: req.body.title,
note: req.body.note
});
foundUser.save((err) => {
if (err) throw err;
res.redirect('/notes');
});
}
});
});
app.post('/update', (req, res) => {
// console.log(req.body);
User.findById(req.user._id, (err, foundUser) => {
if (err) throw err;
if (foundUser) {
let length = foundUser.notes.length;
let i = 0;
while (i < length) {
if (foundUser.notes[i]._id == req.body.id) {
foundUser.notes[i].title = req.body.title;
foundUser.notes[i].note = req.body.note;
break;
}
i++;
}
foundUser.save(err => {
if (err) throw err;
else {
res.redirect('/notes');
}
});
}
});
});
app.post('/delete', (req, res) => {
console.log('delete');
User.findById(req.user._id, (err, foundUser) => {
if (err) throw err;
if (foundUser) {
let length = foundUser.notes.length;
let i = 0;
while (i < length) {
if (foundUser.notes[i]._id == req.body.id) {
index = i;
break;
}
i++;
}
foundUser.notes.splice(index, 1);
foundUser.save(err => {
if (err) throw err;
else {
res.redirect('notes');
}
});
}
});
});
app.get('/logout', function (req, res) {
req.logout();
res.redirect('/home');
});
//Listen to port
app.listen(3000, (err) => {
if (!err) {
console.log('Server started on port 3000');
}
});