-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRouter.js
More file actions
131 lines (102 loc) · 3.28 KB
/
Copy pathRouter.js
File metadata and controls
131 lines (102 loc) · 3.28 KB
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
const bcrypt = require('bcrypt');
class Router{
constructor(app, db){
this.login(app, db);
this.logout(app, db);
this.isLoggedIn(app, db);
}
login(app, db){
app.post('/login',(req, res) => {
var username = req.body.username;
var password = req.body.password;
username = username.toLowerCase();
if(username.length > 12 || password.length > 12){
res.json({
success:false,
msg: 'An error occured, Plz try again.'
});
return;
}
var cols = [username];
db.query('SELECT * FROM user WHERE username =? LIMIT 1', cols, (err, data, fields) => {
if(err){
res.json({
success:false,
msg: 'An error occured, Plz try again.'
});
return;
}
if(data && data.length === 1){
bcrypt.compare(password, data[0].password, (bcryptErr, verified) => {
if(verified){
req.session.userID = data[0].id;
res.json({
success:false,
username: data[0].username
});
return;
}
else{
res.json({
success:false,
msg: 'Invalid Password. Plz try again.'
});
return;
}
});
}
else{
res.json({
success:false,
msg: 'User Not Found. Plz try again.'
});
return;
}
});
});
}
logout(app, db){
app.post('/logout', (req, res) => {
if(req.session.userID){
req.session.destroy();
res.json({
success:true
})
return true;
}
else{
res.json({
success:false
})
return false;
}
});
}
isLoggedIn(app, db){
app.post('/isLoggedIn', (req, res) =>{
if(req.session.userID){
var cols = [req.session.userID];
db.query('SELECT * FROM user WHERE id = ? LIMIT 1', cols, (err, data, fields) => {
if(data && data.length === 1){
res.json({
success:true,
username: data[0].username
})
return true;
}
else{
res.json({
success:true
})
}
});
}
else{
res.json({
success:false
})
}
});
}
}
module.exports = Router;