-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
379 lines (305 loc) · 9.85 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
372
373
374
375
376
377
378
379
//jshint esversion:6
import { copyFileSync } from 'fs';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
require('dotenv').config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
//require google strategy
const findOrCreate = require("mongoose-findorcreate");
const { google } = require("googleapis");
const nodemailer = require("nodemailer");
const { oauth2 } = require("googleapis/build/src/apis/oauth2");
import fetch from 'node-fetch';
import { dirname } from 'path';
// 1 TODO: Require all these 3 packages, we don't have to require "passport-local" as it is already included with "passport-local-mongoose"
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
const app = express();
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended: false}));
// 2 TODO: use session here
app.use(session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 432000000,
httpOnly: true
}
}));
// 3 TODO: Initialize passport once we are done with "express-session"
app.use(passport.initialize());
// 4 TODO: Now we have to make passport use the "express-session" module
app.use(passport.session());
//database stuff
mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true, useUnifiedTopology: true});
// new mongoose.Schema({}) is must if we want to use plugins with schemas.
const userSchema = new mongoose.Schema({
email: String,
password: String,
name: String,
phone: String,
admissionDetails: Object,
});
// 5 TODO: use userSchema with "passport-local-mongoose" to hash and salt password.
userSchema.plugin(passportLocalMongoose);
userSchema.plugin(findOrCreate);
//create the above userSchema before creating new mongoose model
const User = new mongoose.model("User", userSchema);
// 6 TODO: Finally we will use "passport-local" module to create and read cookies
passport.use(User.createStrategy());
//below is the older way to serialize and deserialize
// passport.serializeUser(User.serializeUser());
// passport.deserializeUser(User.serializeUser());
passport.serializeUser(function(User, done) {
done(null, User.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function (err, User) {
done(err, User);
});
});
//database stuff ends
//google oauth2 and nodemailer messging setup
const oAuth2Client = new google.auth.OAuth2(process.env.CLIENT_ID, process.env.CLIENT_SECRET, process.env.REDIRECT_URI);
oAuth2Client.setCredentials({ refresh_token: process.env.REFRESH_TOKEN});
async function sendMail(senderName, senderEmail, senderSubject, senderQuery){
try{
const ACCESS_TOKEN = await oAuth2Client.getAccessToken();
const transport = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: '[email protected]',
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
refreshToken: process.env.REFRESH_TOKEN,
accessToken: ACCESS_TOKEN,
},
});
const mailOptions = {
from: "Homecool Bot <[email protected]>",
to: "[email protected]",
subject: `${senderEmail} sent you a message`,
text: `Message from ${senderName}:\n Subject: ${senderSubject}\n Query: ${senderQuery}`,
}
const result = await transport.sendMail(mailOptions);
return result;
}catch (error) {
return error;
}
}
async function notification( formData, sendTo ){
try{
const ACCESS_TOKEN = await oAuth2Client.getAccessToken();
const transport = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: '[email protected]',
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
refreshToken: process.env.REFRESH_TOKEN,
accessToken: ACCESS_TOKEN,
},
});
const mailOptions = {
from: "Homecool Bot <[email protected]>",
to: sendTo,
subject: `You application for Admission at Homecool was submitted sucessfully!`,
text: `Details Received:\n\n ${formData}`,
}
const result = await transport.sendMail(mailOptions);
return result;
}catch (error) {
return error;
}
}
//setup to fetch blog data from hashnode api and display on the home page
async function fetchHashnodeBlog(){
const variables = { page: 0 };
const query = `
query GetUserArticles($page: Int!) {
user(username: "ankitkp028") {
publication {
posts(page: $page) {
title
brief
slug
coverImage
dateAdded
}
}
}
}
`;
const data = await fetch("https://api.hashnode.com/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query,
variables,
}),
});
const result = await data.json();
//the actual post is nested deep down in the result object
const articles = result.data.user.publication.posts;
return articles;
}
//get requests
app.get("/", (req, res) => {
if(req.isAuthenticated()){
res.redirect("/home");
}else{
res.render("index");
}
});
app.get("index", (req, res) => {
res.render("index");
});
app.get("/signup", (req, res) => {
if(req.isAuthenticated()){
res.redirect("home");
}else{
res.render("signup");
}
});
app.get("/signin", (req, res) => {
if(req.isAuthenticated()){
res.redirect("home");
}else{
res.render("signin");
}
});
app.get("/home", async (req, res) => {
if(req.isAuthenticated()){
const blogList = await fetchHashnodeBlog();
res.render("home", {blogList: blogList});
}else{
res.redirect("/index")
}
});
app.get("/logout", (req, res) => {
req.logout(function(err) {
if (err) {
console.log("An error occured while logging out");
}else{
res.redirect('/');
}
});
});
app.get("/account", (req, res) => {
if(req.isAuthenticated()){
let name = req.user.name;
let email = req.user.username;
let phone = req.user.phone;
res.render("account", {name: name, email: email, phone: phone});
}else{
res.redirect("/")
}
});
app.get("/admission-form", (req, res) => {
if(req.isAuthenticated()){
res.render("admission_form");
}else{
res.redirect("/")
}
});
app.get("*", (req, res) => {
res.render("404");
});
app.post("/signup", (req, res) => {
let name = req.body.name;
let phone = req.body.phone;
User.register({username: req.body.username}, req.body.password, (err, user) => {
if(!err){
passport.authenticate("local")(req, res, function(){
User.findById(user._id, (err, foundUser) => {
if(err){
console.log(err);
} else{
if(foundUser){
foundUser.name = req.body.name,
foundUser.phone = req.body.phone,
console.log(req.body.phone);
foundUser.save(() => {
res.redirect("/home");
});
}
}
});
// res.redirect("/home")
});
} else {
console.log("error in registering");
res.redirect("/signup")
}
});
});
app.post("/signin", (req, res) => {
const user = new User({
username: req.body.username,
password: req.body.password
});
req.login(user, function(err) {
if (err) {
console.log("An error occured while logging in");
res.redirect('/signin');
}else{
passport.authenticate("local")(req, res, ()=>{
res.redirect('/home');
});
}
});
});
app.post("/ask-a-teacher", (req, res) => {
let senderName = req.body.name;
let senderEmail = req.body.email;
let senderStreamSem = req.body.streamSem;
let senderQuery = req.body.query;
//sending automatic email from custom sendMail function...
sendMail(senderName, senderEmail, senderStreamSem, senderQuery).then(result => res.redirect("/"))
.catch(error => console.log(error.message));
});
app.post("/admission-form", (req, res) => {
let sendTo = req.user.username;
let admData = {
applicantName: req.body.name,
applicantEmail: req.body.email,
applicantPhone: req.body.phone,
applicantDOB: req.body.dob,
applicantGender: req.body.gender,
applicantFathername: req.body.fathername,
applicantFatheroccupation: req.body.fatheroccupation,
applicantMothername: req.body.mothername,
applicantMotheroccupation: req.body.motheroccupation,
applicantAddress: req.body.address,
applicantTenthpercentage: req.body.tenmark,
applicantTwelthpercentage: req.body.twelvemark,
applicantStream: req.body.stream,
}
User.findById(req.user.id, (err, foundUser) => {
if(err){
console.log(err);
} else{
if(foundUser){
foundUser.admissionDetails = admData,
foundUser.save(() => {
//sending automatic email from custom sendMail function...
notification( JSON.stringify(admData), sendTo ).then(result => res.redirect("/home"))
.catch(error => console.log(error.message));
});
}
}
});
});
app.listen(process.env.PORT || 3000, "0.0.0.0", () => {
console.log("Server started on port 3000");
});