-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
376 lines (272 loc) · 8.4 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
var express = require("express");
var bodyParser = require("body-parser");
var request = require("request");
var mongoose = require("mongoose");
var app = express();
var Student = require("./models/student");
var Club = require("./models/club");
var passport = require("passport");
// var LocalStrategy = require("passport-local");
const LocalStrategy = require("passport-local").Strategy;
var passportLocalMongoose = require("passport-local-mongoose");
var methodOverride = require("method-override");
const accountSid = '----';
const authToken = '---';
const client = require('twilio')(accountSid, authToken);
mongoose.connect('mongodb://localhost:27017/Publicize', { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false });
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(methodOverride("_method"));
app.use(require("express-session")({
secret: "This the secret message for authentication",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use('student-signup', new LocalStrategy(Student.authenticate()));
passport.serializeUser(Student.serializeUser());
passport.deserializeUser(Student.deserializeUser());
passport.use('club-signup', new LocalStrategy(Club.authenticate()));
passport.serializeUser(Club.serializeUser());
passport.deserializeUser(Club.deserializeUser());
app.use(function (req, res, next) {
res.locals.currentUser = req.user;
next();
});
// ----------------------------------------------
const fs = require("fs");
const multer = require("multer");
const OAuth2Data = require("./credentials.json");
var name, pic
const { google } = require("googleapis");
const CLIENT_ID = OAuth2Data.web.client_id;
const CLIENT_SECRET = OAuth2Data.web.client_secret;
const REDIRECT_URL = OAuth2Data.web.redirect_uris[0];
const oAuth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URL
);
var authed = false;
// If modifying these scopes, delete token.json.
const SCOPES =
"https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/userinfo.profile";
app.set("view engine", "ejs");
var Storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, "./images");
},
filename: function (req, file, callback) {
// callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
callback(null, file.originalname);
},
});
var upload = multer({
storage: Storage,
}).single("file"); //Field name and max count
// ----------------------------------------------
// ----------- Routes --------------------
//------------------------ Student Authentication Routes -------------------------
app.get("/student_login", function (req, res) {
res.render("student_login.ejs");
});
app.get("/student_signup", function (req, res) {
res.render("student_signup.ejs");
});
app.post("/student_signup", function (req, res) {
Student.register(
new Student({
username: req.body.username,
name: req.body.name,
year: req.body.year,
clubs: req.body.clubs,
phone: req.body.phone
}),
req.body.password, function (err, user) {
if (err) {
console.log(err);
}
passport.authenticate("student-signup")(req, res, function () {
res.redirect("/");
});
});
});
app.post("/student_login", passport.authenticate("student-signup", {
successRedirect: "/",
failureRedirect: "/student_login"
}), function (req, res) {
});
app.get("/logout", function (req, res) {
req.logout();
res.redirect("/");
});
// ------------------------Student Authentication Ends ------------------------------
// --------------------- Club Authentication ----------------
app.get("/club_login", function (req, res) {
res.render("club_login.ejs");
});
app.get("/club_signup", function (req, res) {
res.render("club_signup.ejs");
});
app.post("/club_signup", function (req, res) {
Club.register(
new Club({
username: req.body.username,
name: req.body.name,
description: req.body.description,
phone: req.body.phone
}),
req.body.password, function (err, user) {
if (err) {
console.log(err);
}
passport.authenticate("club-signup")(req, res, function () {
res.redirect("/");
});
});
});
app.post("/club_login", passport.authenticate("club-signup", {
successRedirect: "/",
failureRedirect: "/club_login"
}), function (req, res) {
});
app.get("/logout", function (req, res) {
req.logout();
res.redirect("/");
});
// --------------------- Club Authentication ends----------------
// ---------------------- Messaging Routes ----------------------
app.get("/club/message", function (req, res) {
if (!authed) {
// Generate an OAuth URL and redirect there
var url = oAuth2Client.generateAuthUrl({
access_type: "offline",
scope: SCOPES,
});
console.log(url);
console.log(location);
console.log("YEH WALI LOCATION....")
res.render("club_dashboard", { url: url });
} else {
var oauth2 = google.oauth2({
auth: oAuth2Client,
version: "v2",
});
oauth2.userinfo.get(function (err, response) {
if (err) {
console.log(err);
} else {
console.log(response.data);
name = response.data.name
pic = response.data.picture
res.render("success", {
name: response.data.name,
pic: response.data.picture,
success: false
});
}
});
}
// res.render("club_dashboard.ejs");
});
app.post("/club/message/send", function (req, res) {
Student.find({}, function (err, details) {
if (err) {
console.log(err);
} else {
console.log(req.body.url)
details.forEach(function (info) {
if (req.body.year.includes(info.year) && info.clubs.includes(req.user.username)) {
client.messages
.create({
body: req.body.message,
from: 'whatsapp:+14155238886',
to: 'whatsapp:+91' + info.phone,
mediaUrl: req.body.url
})
.then(message => console.log(message.sid))
.done();
}
});
res.redirect("/club/message");
}
});
});
// ---------------------- Messaging Routes End -----------------
app.get("/", function (req, res) {
res.render("home.ejs");
});
// --------------------- SANJITCODES----------------------
// app.get("/", (req, res) => {
// });
var location;
app.post("/club/message/upload", (req, res) => {
upload(req, res, function (err) {
if (err) {
console.log(err);
return res.end("Something went wrong");
} else {
console.log(req.file.path);
const drive = google.drive({ version: "v3", auth: oAuth2Client });
const fileMetadata = {
name: req.file.filename,
};
location = req.file.filename;
const media = {
mimeType: req.file.mimetype,
body: fs.createReadStream(req.file.path),
};
drive.files.create(
{
resource: fileMetadata,
media: media,
fields: "id",
},
(err, file) => {
if (err) {
// Handle error
console.error(err);
} else {
// fs.unlinkSync(req.file.path)
res.render("success", { name: name, pic: pic, success: true })
}
}
);
}
});
});
app.get('/club/message/logout', (req, res) => {
authed = false
console.log("LOGOUT BOTTON LOGOUT BOTTON LOGOUT BOTTON LOGOUT BOTTON LOGOUT BOTTON LOGOUT BOTTON LOGOUT BOTTON LOGOUT BOTTON")
res.redirect('/club/message')
})
app.get("/google/callback", function (req, res) {
const code = req.query.code;
if (code) {
// Get an access token based on our OAuth code
oAuth2Client.getToken(code, function (err, tokens) {
if (err) {
console.log("Error authenticating");
console.log(err);
} else {
console.log("Successfully authenticated");
console.log(tokens)
oAuth2Client.setCredentials(tokens);
authed = true;
res.redirect("/club/message");
}
});
}
});
//---------------------SANJITCODES ENDS----------------------
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect("/login");
}
var port = process.env.PORT || 5000;
app.listen(port, function () {
console.log("Server Has Started!!");
});