-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthentication.js
277 lines (267 loc) · 6.77 KB
/
authentication.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
const con = require('./config');
const bcrypt = require('bcrypt');
const bodyParser = require('body-parser');
exports.create = function (req,res) {
const event = req.body.event;
const name = req.body.name;
const email = req.body.email;
const password = req.body.pass;
const mobile = Number(req.body.mobile);
var event1= event+"_committee";
var user={
event:req.body.event,
name:req.body.name,
email:req.body.email,
committee : 'head'
};
var que = `CREATE TABLE ${event}(id int NOT NULL AUTO_INCREMENT,name varchar(30) NOT NULL,email varchar(50) NOT NULL,password varchar(200) NOT NULL,mobile float(15,0),committee varchar(20) NOT NULL,points INT(3) NOT NULL DEFAULT 0,PRIMARY KEY(id))`;
con.query(que,function(qerr,qres){
if(qerr){
console.log(qerr);
}
});
que = `CREATE TABLE ${event1}(committee_name varchar(20) NOT NULL)`;
con.query(que,function(qerr,qres){
if(qerr){
console.log(qerr);
}
});
que = `CREATE TABLE ${event}_work(heading varchar(100) NOT NULL UNIQUE,description varchar(200),committee varchar(20) NOT NULL ,assignee varchar(30),difficulty varchar(10) NOT NULL,created DATETIME,complete boolean NOT NULL DEFAULT 0,proposed INT(2) NOT NULL DEFAULT 0)`;
con.query(que,function(qerr,qres){
if(qerr){
console.log(qerr);
}
});
que = `INSERT INTO events values('${event}')`;
con.query(que,function(qerr,qres){
if(qerr){
console.log(qerr);
}
});
const SALT_FACTOR = 5;
bcrypt.genSalt(SALT_FACTOR,function(serr,salt){
if(serr){
console.log(serr);
}
bcrypt.hash(password,salt,function(herr,hash){
if(herr){
console.log(herr);
}
que = `INSERT INTO ${event} (name,email,password,mobile,committee) VALUES('${name}','${email}','${hash}',${mobile},'head')`;
con.query(que,function(qerr,qres){
if(qerr){
console.log(qerr);
}
req.session.user = user;
res.redirect('/main');
});
});
});
//console.log("success!!");
}
exports.signup = function(req,res){
const event = req.body.event;
const name= req.body.name;
const email = req.body.email;
const password = req.body.pass;
const mobile = Number(req.body.mobile);
const committee = req.body.committee;
var user={
event:req.body.event,
name:req.body.name,
email:req.body.email,
committee : committee
};
const SALT_FACTOR = 5;
bcrypt.genSalt(SALT_FACTOR,function(serr,salt){
if(serr){
console.log(serr);
}
bcrypt.hash(password,salt,function(herr,hash){
if(herr){
console.log(herr);
}
que = `INSERT INTO ${event} (name,email,password,mobile,committee) VALUES('${name}','${email}','${hash}',${mobile},'${committee}')`;
con.query(que,function(qerr,qres){
if(qerr){
console.log(qerr);
}
req.session.user = user;
if(committee=='head'){
res.redirect('/main');
}
else{
res.redirect('/committee/'+committee);
}
});
});
});
// console.log(event);
}
exports.login = function(req,res){
var event = req.body.event;
var email = req.body.email;
var password = req.body.pass;
var user={
event:req.body.event,
email:req.body.email
};
var que=`SELECT * FROM ${event} WHERE email = ?`;
con.query(que,[email], function (error, results, fields) {
if (error) {
console.log(error);
res.redirect('/login');
}
else{
if(results.length >0){
bcrypt.compare(password, results[0].password, function(err, doesMatch){
if (doesMatch){
user.committee = results[0].committee;
user.name = results[0].name;
req.session.user = user;
if(results[0].committee=='head'){
res.redirect('/main');
}
else{
res.redirect('/committee/'+results[0].committee);
}
}
else{
res.redirect('/login?err=Enter valid Password');
}
});
}
else{
res.redirect('/login?err=Enter valid Email');
}
}
// console.log("Success");
});
}
exports.logout = function(req,res){
req.session.destroy();
// console.log(req.session);
res.redirect('/log');
// console.log('Logged out');
}
exports.eventVal = function(req,res){
let que = "select name from events";
let input = req.query.e.toLowerCase();
con.query(que,function(err,results,fields){
if(err){
console.log(err);
}
let check = results.map((obj) => {
return obj.name.toLowerCase();
});
if(input.indexOf(' ')>=0){
res.send('Space is not allowed...')
}
else if(check.indexOf(input)>=0){
res.send('Name already exists...');
}
else{
res.send();
}
});
}
exports.userVal = function(req,res){
let que = `select name from ${req.query.e}`;
let input = req.query.u.toLowerCase();
con.query(que,function(err,results,fields){
if(err){
console.log(err);
}
let check = results.map((obj) => {
return obj.name.toLowerCase();
});
if(check.indexOf(input)>=0){
res.send('Name already taken...');
}
else{
res.send();
}
});
}
exports.committeeVal = function(req,res){
let que = `select committee_name as name from ${req.query.e}_committee`;
let input = req.query.u.toLowerCase();
con.query(que,function(err,results,fields){
if(err){
console.log(err);
}
let check = results.map((obj) => {
return obj.name.toLowerCase();
});
if(input.indexOf(' ')>=0){
res.send('Space is not allowed...')
}
else if(check.indexOf(input)>=0){
res.send('Committee already exists...');
}
else{
res.send();
}
});
}
exports.assignee = function(req,res){
let que = `select name from ${req.session.user.event} where committee='${req.query.c}' AND name like '${req.query.a}%'`;
con.query(que,function(err,results,fields){
if(err){
console.log(err);
}
let response = results.map((x) => {
return x.name;
});
res.send(response);
});
}
exports.deleteEvent = function(req,res){
if(req.session.user.event == req.body.event){
let event = req.session.user.event;
let que = `DROP TABLE IF EXISTS ${event},${event}_committee,${event}_work`;
con.query(que,function(err,results,fields){
if(err){
console.log(err);
}
que = `DELETE FROM events WHERE name='${event}'`;
con.query(que,function(err,results,fields){
if(err){
console.log(err);
}
res.redirect('/logout');
});
});
}
else{
res.redirect('/main');
}
}
exports.deleteCommittee = function(req,res){
if(req.session.user && req.session.user.committee == 'head'){
let event = req.session.user.event;
let committee = req.body.committee;
let que = `DELETE FROM ${event} WHERE committee='${committee}'`;
con.query(que,function(err,results,fields){
if(err){
console.log(err);
}
});
que = `DELETE FROM ${event}_committee WHERE committee_name='${committee}'`;
con.query(que,function(err,results,fields){
if(err){
console.log(err);
}
});
que = `DELETE FROM ${event}_work WHERE committee='${committee}'`;
con.query(que,function(err,results,fields){
if(err){
console.log(err);
}
res.redirect('/main');
});
}
else{
req.redirect('/logout');
}
}