-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
224 lines (187 loc) · 5.3 KB
/
index.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
var express = require('express'),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
passport = require('passport'),
localStrategy = require('passport-local'),
passportLocalMongoose = require('passport-local-mongoose'),
methodOverride=require("method-override"),
flash=require('connect-flash'),
User = require("./models/user"),
Company=require("./models/Company");
app = express();
//Connect to DB
//mongoose.connect('mongodb://localhost/VisionFirst');
mongoose.connect('mongodb://'+process.env.COSMOSDB_USERNAME+'.documents.azure.com:10255/visionfirst?ssl=true', {
auth: {
user: process.env.COSMOSDB_USERNAME,
password: process.env.COSMOSDB_PASSWORD
}
})
.then(() => console.log('connection successful'))
.catch((err) => console.error(err));
app.set('view engine', "ejs");
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(flash());
app.use(methodOverride('_method'));
//Serve Resources
app.use(express.static("public"));
app.use(require('express-session')({
secret: 'sadasfdsagrgawrgerw',
resave: false,
saveUninitialized: false
}));
app.use((req,res,next)=>{
res.locals.error=req.flash("error");
res.locals.success=req.flash("success");
next();
});
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//MiddleWare To provide UserData to all templates
app.use(function(req,res,next){
res.locals.currentUser=req.user;
next();
});
//MiddleWare to Restrict access with log In
function isLoggedIn(req,res,next){
if(req.isAuthenticated()){
return next();
}
req.flash("error","Please Login First!");
res.redirect('/login');
}
//ROUTES
//Login Page
app.get('/', (req, res) => {
res.redirect('/login');
});
app.get('/login', (req, res) => {
res.render('login');
});
//Logout Route
app.get("/logout",(req,res)=>{
req.logout();
req.flash("success","Logged Out");
res.redirect('/login');
});
//Authenticate User then login
app.post('/login', passport.authenticate("local", {
successRedirect: '/AdminView',
failureRedirect: '/login',
failureFlash:true
}), (req, res) => {
});
//Register A New User
app.get('/Register', (req, res) => {
res.render("register");
});
app.post('/Register',(req,res)=>{
if(req.body.password==req.body.confirmpassword){
User.register(new User({username:req.body.username,Role:req.body.role,email:req.body.email,mobile:req.body.mobile}),req.body.password,(err,user)=>{
if(err){
return res.render('register');
}
passport.authenticate("local")(req,res,()=>{
res.redirect('/AdminView');
});
});
}else{
res.render("register");
}
});
//User View Based of User Role
app.get('/AdminView',isLoggedIn, (req, res) => {
if(req.user.Role=="IT_ADMIN"){
res.render('AdminView');
}
else{
res.redirect("/Company/new");
}
});
app.get('/Company/new',isLoggedIn, (req, res) => {
res.render("CreateCompany");
});
//CRUD On CompanySchema Routes
app.get("/Company",(req,res)=>{
Company.find({},(err,companies)=>{
if(err){
res.send(err);
}else{
res.send(companies);
}
})
});
app.post('/Company/new',isLoggedIn,(req,res)=>{
if(req.user.Role=="IT_ADMIN"){
req.body.Company.Approved=true;
}
Company.create(req.body.Company,(err,newCompany)=>{
if(err){
console.log(err);
}else{
res.redirect("/Company/new");
}
});
});
app.get('/Company/:id/edit',isLoggedIn,(req,res)=>{
Company.findById(req.params.id,(err,foundCompany)=>{
if(err){
res.redirect("/AdminView");
}else{
res.render("EditCompany",{company:foundCompany});
}
})
})
app.put("/Company/:id",isLoggedIn,(req,res)=>{
Company.findByIdAndUpdate(req.params.id,req.body.Company,(err,newBlog)=>{
if(err){
console.log(err);
}else{
console.log(newBlog);
res.redirect("/AdminView");
}
});
});
app.put("/Company/:id/approve",isLoggedIn,(req,res)=>{
Company.findById(req.params.id,(err,foundCompany)=>{
if(err){
console.log("error");
}else{
foundCompany.Approved=true;
Company.findByIdAndUpdate(req.params.id,foundCompany,(err,updatedCompany)=>{
if(err){
console.log("error");
}else{
res.render("AdminView");
}
})
}
})
})
app.delete("/Company/:id",isLoggedIn,(req,res)=>{
Company.findByIdAndRemove(req.params.id,(err,deletedCompany)=>{
if(err){
console.log(err);
}else{
console.log(deletedCompany);
res.render("AdminView");
}
});
});
//Redirect to login page if request doesnot match any other route
app.get("*", (req, res) => {
if(!req.isAuthenticated){
res.redirect('/login');
}else{
res.redirect("/AdminView");
}
});
//Start Server
app.listen(process.env.PORT || 5050, () => {
console.log("Server Started");
});