-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
222 lines (193 loc) · 5.8 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
var express = require('express'),
app = express(),
PORT = 5000 || process.env.port,
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
methodOverride = require('method-override');
//Importing passport libraries
var passport = require('passport'),
localStrategy = require('passport-local'),
passportLocalMongoose = require('passport-local-mongoose');
app.set("view engine","ejs");
app.use(express.static("assets"));
app.use(methodOverride("_method"));
app.use(bodyParser.urlencoded({extended:true}));
mongoose.connect("mongodb://localhost/event_db");
//=====================
// MODELS
//=====================
//User Model
var userSchema = new mongoose.Schema({
username : String,
password : String,
club_name : String,
club_head_name : String,
contact : Number,
email : String
},{timestamps : true});
userSchema.plugin(passportLocalMongoose);
var User = mongoose.model("User", userSchema);
//Application model
var appSchema = new mongoose.Schema({
to : String,
from : String,
subject : String,
purpose : String,
timing : String,
description : String,
applicant : String,
author : {
id : {
type : mongoose.Schema.Types.ObjectId,
rel : "User"
},
username : String,
email : String
},
isVerif1 : Boolean,
isVerif2 : Boolean
}, {timestamps : true});
var Application = mongoose.model("Application", appSchema);
//Express-session setup
app.use(require("express-session")({
secret : "I am AJ",
resave : false,
saveUninitialized : false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(function(req, res, next){
res.locals.currentUser = req.user;
next();
})
//=====================
// ROUTES
//=====================
//Home route
app.get("/", function(req, res){
res.render("home");
});
//Application route
app.get("/application", function(req, res){
User.find({}, function(err, foundUser){
if(err)
console.log(err);
else
res.render("application", {user : foundUser});
});
});
app.get("/application/new", function(req, res){
res.render("newapplication");
});
app.post("/application", function(req, res){
Application.create({
to : req.body.to,
from : req.body.from,
subject : req.body.subject,
purpose : req.body.purpose,
timing : req.body.timing,
description : req.body.description,
applicant : req.body.applicant,
author : {
id : req.user._id,
username : req.user.username,
email : req.user.email
},
isVerif1 : false,
isVerif2 : false
}, function(err, foundApp){
if(err)
console.log(err);
else
{
foundApp.save();
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: '[email protected]',
from: '[email protected]',
subject: 'Sending with SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html : '<form action="http://localhost:5000/application/verify1/'+foundApp._id+'"?_method=PUT" method="POST" enctype="multipart/form-data"><button type = "submit">Approve</button></form>'
};
sgMail.send(msg);
res.redirect("/");
}
});
});
app.get("/application/view",function(req,res){
Application.find({}, function(err, foundApp){
if(err)
console.log(err);
else
res.render("viewapplication", {applications : foundApp});
});
});
//Show route
app.get("/application/view/:id", function(req, res){
Application.findById(req.params.id, function(err, foundApp){
if(err)
console.log(err);
else
res.render("view", {application : foundApp});
});
});
app.post("/application/verify1/:id", function(req, res){
// console.log("PUT Here");
Application.findById(req.params.id, function(err, foundApp){
if(err)
{
console.log(err);
}
else
{
foundApp.isVerif1 = true;
foundApp.save();
res.redirect('/');
}
})
});
// AUTH ROUTES
//Register Route
app.get("/register", function(req, res){
res.render("register");
});
app.post("/register", function(req, res){
var user = new User({username : req.body.username, club_name : req.body.club_name, club_head_name : req.body.club_head_name, contact : req.body.contact, email : req.body.email});
User.register(user, req.body.password, function(err, newUser){
if(err)
console.log(err);
else
{
passport.authenticate("local")(req, res, function(){
if(err)
console.log(err);
else
res.redirect("/");
// res.send("success");
});
}
});
});
//Login Route
app.get("/login", function(req, res){
res.render("login");
});
app.post("/login", passport.authenticate("local",{
successRedirect : "/",
failureRedirect : "/login"
}));
//Logout Route
app.get("/logout", function(req, res){
req.logout();
res.redirect("/login");
})
app.listen(PORT, function(err){
if(err)
console.log(err);
else
console.log("Server started at PORT : "+PORT);
});