-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.js
More file actions
190 lines (160 loc) · 5.5 KB
/
server.js
File metadata and controls
190 lines (160 loc) · 5.5 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
// Main server file
const express = require('express');
const path = require('path');
require('./db/mongoose');
const bodyParser = require('body-parser');
const Report = require('./models/reportModel');
const reportRoutes = require('./routes/reportRoutes');
const adminRoutes = require('./routes/adminRoutes');
const userRoutes = require('./routes/userRoutes');
const Admin = require('./models/adminModel');
const User = require('./models/userModel');
const passport = require('passport');
var LocalStrategy = require('passport-local');
var middleware = require('./middleware/auth');
const app = express();
const PORT = process.env.PORT || 5000;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const public = path.join(__dirname, 'client');
app.use(express.static(public));
//Passport configuration
app.use(
require('express-session')({
secret: 'best website ever!',
resave: false,
saveUninitialized: false
})
);
app.use(passport.initialize());
app.use(passport.session());
//passport for admin
passport.use('admin', new LocalStrategy(Admin.authenticate()));
//passport for admin
passport.use('user', new LocalStrategy(User.authenticate()));
function SessionConstructor(userId, userGroup, details) {
this.userId = userId;
this.userGroup = userGroup;
this.details = details;
}
passport.serializeUser(function(userObject, done) {
// userObject could be a Model1 or a Model2... or Model3, Model4, etc.
let userGroup = 'admin';
let userPrototype = Object.getPrototypeOf(userObject);
if (userPrototype === Admin.prototype) {
userGroup = 'admin';
} else if (userPrototype === User.prototype) {
userGroup = 'user';
}
let sessionConstructor = new SessionConstructor(userObject.id, userGroup, '');
done(null, sessionConstructor);
});
passport.deserializeUser(function(sessionConstructor, done) {
if (sessionConstructor.userGroup == 'admin') {
Admin.findOne(
{
_id: sessionConstructor.userId
},
'-localStrategy.password',
function(err, user) {
// When using string syntax, prefixing a path with - will flag that path as excluded.
done(err, user);
}
);
} else if (sessionConstructor.userGroup == 'user') {
User.findOne(
{
_id: sessionConstructor.userId
},
'-localStrategy.password',
function(err, user) {
// When using string syntax, prefixing a path with - will flag that path as excluded.
done(err, user);
}
);
}
});
// Routes
// Report routes
app.use('/report', reportRoutes);
// Admin routes
app.use('/admin', adminRoutes);
// User routes
app.use('/user', userRoutes);
//html routes
app.get('/about', (req, res, next) => {
res.sendFile(__dirname + '/client/about.html');
});
app.get('/form', (req, res, next) => {
res.sendFile(__dirname + '/client/form.html');
});
app.get('/report/:id', async (req, res, next) => {
const _id = req.params.id;
try {
const report = await Report.findOne({ _id }, '-imageBuffer');
if (!report) {
return res.status(404).send({ error: 'Report not found' });
}
date1 = new Date(report.createdAt);
date2 = new Date(report.updatedAt);
res.status(200).send(`
<h1>REPORT ID : ${report._id}</h1>
<h5>Created on: ${date1.toLocaleString('en-GB')}</h5>
<h5>Updated on: ${date2.toLocaleString('en-GB')}</h5>
<h5> Status : ${report.status}</h5>
<pre>
<p>
<b>View Image:</b> <a href='/report/image/${
report._id
}' target=_blank>Click here</a>
<b>View Location on Google Map:</b> <a href='https://www.google.com/maps/dir/${
report.geometry.coordinates[1]
},${report.geometry.coordinates[0]}' target=_blank>Click here</a>
<b>Uploader Name:</b> <i>${report.name}</i>
<b>Uploader Contact No.:</b> <i>${report.contactNumber}</i>
<b>Report Type:</b> <i>${report.reportType}</i>
<b>Description:</b> <i>${report.description}</i>
<b>Coordinates:</b> <i>${report.geometry.coordinates}</i>
<b>Landmark Specified:</b> <i>${report.location}</i>
<b>Locality:</b> <i>${report.results.locality}</i>
<b>Pincode:</b> <i>${report.results.pincode}</i>
<b>District:</b> <i>${report.results.district}</i>
<b>Approximated Address:</b> <i>${report.results.formatted_address}</i>
<p>
</pre>
`);
} catch (e) {
res.status(404).send();
}
});
app.get('/userRegister', (req, res, next) => {
res.sendFile(__dirname + '/client/userRegister.html');
});
app.get('/userLogin', (req, res, next) => {
res.sendFile(__dirname + '/client/userLogin.html');
});
app.get('/userDashboard', middleware.isLoggedInUser, (req, res, next) => {
res.sendFile(__dirname + '/users/userDashboard.html');
});
app.get('/adminRegister', (req, res, next) => {
res.sendFile(__dirname + '/client/adminRegister.html');
});
app.get('/adminLogin', (req, res, next) => {
res.sendFile(__dirname + '/client/adminLogin.html');
});
app.get('/dashboard', middleware.isLoggedInAdmin, (req, res, next) => {
res.sendFile(__dirname + '/admins/dashboard.html');
});
app.get('/dashboardReports', middleware.isLoggedInAdmin, (req, res, next) => {
res.sendFile(__dirname + '/admins/dashboardReports.html');
});
app.get('/', (req, res, next) => {
res.sendFile(__dirname + '/client/index.html');
});
const server = app.listen(PORT, () => {
console.log(`Server has started on port ${PORT}`);
});
const io = require('./socket').init(server);
io.on('connection', socket => {
console.log('New client connected');
});