-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
200 lines (176 loc) · 5.56 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
const express = require('express');
const app = express();
//database
const {db, Customer, Booking} = require('./db')
const session = require('express-session');
app.use(session({
secret: 'kdbfefwefascbasvafjdsvblsdjvlsfvsdjk',
resave: false,
saveUninitialized: true
}))
//middlewares for data exchange in proper format, in post requests
app.use(express.urlencoded({extended: true}));
app.use(express.json());
app.set('view engine', 'hbs');
app.use("/", express.static(__dirname + '/public'));
//signup
app.post('/signup', (req,res)=>{
if(req.body.email && req.body.pass && req.body.name && req.body.address && req.body.phone){
Customer.create({
name: req.body.name,
email: req.body.email,
password: req.body.pass,
address: req.body.address,
phone: req.body.phone
}).then((user)=>{
req.session.userID = user.id;
req.session.email = user.email;
req.session.name = user.name;
req.session.phone = user.phone;
req.session.address = user.address;
return res.render('yourbookings');
}).catch((error)=>{
//throw error;
return res.render('error', {error, text: "Some error occured, while signing up! Please try again."});
})
}
else{
return res.render('error', {error: "Please enter all details to sign up!"});
}
})
//login
app.get('/login', (req, res)=>{
if(req.session.email){
res.render('yourbookings');
}
else{
res.render('login');
}
})
app.get('/login_ess', (req, res)=>{
if(req.session.userID){
return res.render('yourbookings');
}
else{
res.render('login', {msg:true});
}
})
app.get('/isloggedin', (req,res)=>{
if(req.session.name){
return res.send("true");
}
else{
return res.send("false")
};
})
app.post('/login', (req,res)=>{
if(req.body.email && req.body.pass){
Customer.findOne({
where: {email: req.body.email}
}).then((user)=>{
if(!user){
return res.render('error', {error: "Account does not exist! Please try logging in again with correct email."});
}
else if(user.password==req.body.pass){
// sab kuch shi hai, then we should create a cookie with userID and redirect to profile
req.session.userID = user.id;
req.session.email = user.email;
req.session.name = user.name;
req.session.phone = user.phone;
req.session.address = user.address;
return res.redirect('/mybookings');
}
else{
return res.render('error', {error: "Invalid E-mail or Password! Please try logging in again with correct details."});
}
}).catch((error)=>{
console.log(error);
return res.render('error', {error, text: "Some error occurred! Please try logging in again."});
})
}
else{
res.render('error', {error: "Please enter all details to login!"});
}
})
//logout
app.get('/logout',(req,res,next)=>{
if(req.session){
req.session.destroy((err)=>{
if(err) return res.render('error', {err});
return res.redirect('/');
})
}
else{
res.redirect('/');
}
})
//accessing user bookings
app.get('/mybookings', (req, res)=>{
//security
if(!req.session.email){
return res.redirect('/login_ess');
}
Booking.findAll({
// where: {domain: req.query.domain},
where: {
email: req.session.email
}
}).then((data)=>{
console.log(data);
return res.render('yourbookings', {data});
}).catch((error)=>{
return res.render('error', {error, text: "Some error occured while loading your bookings. Please try again!"});
})
})
//booking a plan
app.use('/bookplan/', (req, res)=>{
if(!req.session.email){
return res.redirect('/login_ess');
}
res.sendFile(__dirname + "/public/booking.html");
})
//booking a package
app.use('/bookpackage/', (req, res)=>{
if(!req.session.email){
return res.redirect('/login_ess');
}
res.sendFile(__dirname + "/public/booking.html");
})
//request for user data while booking plan/ package
app.get('/senduser/', (req, res)=>{
//add security
res.send(req.session);
})
//booking finalized
app.post('/finalplan/', (req, res)=>{
// req.body.plan = true;
//store booking in db
if(req.body.address && req.body.phone && req.body.date && req.body.people && req.body.amt){
Booking.create({
name: req.body.name,
email: req.body.email,
places: req.body.destname,
people: req.body.people,
cost: req.body.amt,
date: req.body.date,
address: req.body.address,
phone: req.body.phone
}).then(()=>{
console.log(req.body);
return res.render('book_rec', {details: req.body});
}).catch((error)=>{
//throw error;
return res.render('error', {error, text: "Some error occured! Booking failed. Please try again."});
})
}
else{
return res.render('error', {error: "Please enter all details to make a booking."});
}
});
//syncing db
db.sync()
.then(()=>{
app.listen(4444, ()=>{
console.log("Server started successfully at http://localhost:4444 ");
})
})