1
1
const fs = require ( 'fs' ) ;
2
2
const path = require ( 'path' ) ;
3
3
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' ) ;
5
6
6
7
const { log } = console ;
7
8
@@ -10,25 +11,41 @@ const loginHandler = (req, res) => {
10
11
req . on ( 'data' , function ( chunk ) {
11
12
data += chunk ;
12
13
} ) ;
13
- req . on ( 'end' , ( ) => {
14
+ req . on ( 'end' , ( ) => {
14
15
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 ) => {
17
19
if ( err ) console . log ( err )
18
20
// else console.log(res[Object.keys(res)[0]]);
19
21
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
+ }
25
45
}
26
46
} ) ;
27
- res . writeHead ( 302 , {
28
- 'location' : "/Public/login.html" } )
29
- res . end ( ) ;
30
- } )
31
- }
47
+ } ) ;
48
+ } ;
32
49
33
50
const signUpHandler = ( req , res ) => {
34
51
log ( 'sign up handler' )
@@ -105,9 +122,5 @@ module.exports = {
105
122
staticHandler,
106
123
signUpHandler,
107
124
loginHandler,
108
- < << << << HEAD
109
125
listHandler
110
126
}
111
- = = === ==
112
- }
113
- >>> >>> > 0245 de3e6dbbc79c3ba481b88a2f91cd573492c8
0 commit comments