Skip to content

Commit 7dff52a

Browse files
author
Nicos
committed
add login handler
Relates to #2
1 parent 8f28683 commit 7dff52a

9 files changed

+37
-24
lines changed

Queries/check_user_password.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
const dbConnection = require("../database/db_connection.js");
1+
const dbConnection = require("../Src/Database/db_connection.js");
22

3-
const check_user_password = (username, cb) => {
3+
const check_user_password = (username, password, cb) => {
44
dbConnection.query(
5-
`SELECT password FROM users WHERE username=$1`,
6-
[username],
5+
`SELECT CASE WHEN EXISTS(SELECT password FROM users WHERE username = $1 AND password = $2) THEN CAST (true AS BOOLEAN) ELSE CAST (false AS BOOLEAN) END`,
6+
[username, password],
77
(err, res) => {
88
if (err) cb(err);
9-
else cb(null, res);
9+
else cb(null, res.rows);
1010
}
1111
);
1212
};
1313

14-
module.exports = check_user_password;
14+
module.exports = check_user_password;

Src/handler.js

+31-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const fs = require('fs');
22
const path = require('path');
33
const qs = require('querystring');
4-
const check_user_exists = require('../Queries/check_username')
4+
const check_user_exists = require('../Queries/check_username');
5+
const check_user_password = require('./../Queries/check_user_password');
56

67
const { log } = console;
78

@@ -10,25 +11,41 @@ const loginHandler = (req, res) => {
1011
req.on('data', function(chunk){
1112
data += chunk;
1213
});
13-
req.on('end',() =>{
14+
req.on('end',() => {
1415
const info = qs.parse(data);
15-
check_user_exists(info.username, (err,res) => {
16-
var stringify = JSON.stringify(res[0])
16+
const username = info.username;
17+
const password = info.password;
18+
check_user_exists(username, (err,response) => {
1719
if (err) console.log(err)
1820
// else console.log(res[Object.keys(res)[0]]);
1921
else {
20-
console.log(res[0]);
21-
console.log(typeof res[0]);
22-
console.log(res[0].case);
23-
24-
22+
const boolean = response[0].case;
23+
if (boolean === true) {
24+
check_user_password(username, password, (err, response) => {
25+
// console.log(res);
26+
const boolean = response[0].case;
27+
if(err){
28+
res.writeHead(500, {'Content-Type':'text/html'});
29+
res.end("<h1> Can't log in at this time</h1>");
30+
} else {
31+
if(boolean === false ){
32+
res.writeHead(500, {'Content-Type':'text/html'});
33+
res.end("<h1>Incorrect password</h1>");
34+
} else if (boolean === true){
35+
// console.log("Success");
36+
res.writeHead(200, {'Content-Type':'text/html'});
37+
res.end("<h1>Success</h1>");
38+
// token = jwt.sign({'logged-in' : 'true', 'username' : `${username}`}, secret);
39+
// response.writeHead(200, {
40+
// "Content-Type": "text/html", 'Set-Cookie' : `Token = ${token}; HttpOnly; Max-Age=9000;`
41+
}
42+
}
43+
});
44+
}
2545
}
2646
});
27-
res.writeHead(302,{
28-
'location': "/Public/login.html"})
29-
res.end();
30-
})
31-
}
47+
});
48+
};
3249

3350
const signUpHandler = (req, res) => {
3451
log('sign up handler')
@@ -105,9 +122,5 @@ module.exports = {
105122
staticHandler,
106123
signUpHandler,
107124
loginHandler,
108-
<<<<<<< HEAD
109125
listHandler
110126
}
111-
=======
112-
}
113-
>>>>>>> 0245de3e6dbbc79c3ba481b88a2f91cd573492c8
File renamed without changes.
File renamed without changes.

Public/login.js public/login.js

File renamed without changes.
File renamed without changes.
File renamed without changes.

Public/signup.js public/signup.js

File renamed without changes.

Public/style.css public/style.css

File renamed without changes.

0 commit comments

Comments
 (0)