diff --git a/public/index.html b/public/index.html index 2ea7e61..c9bd619 100644 --- a/public/index.html +++ b/public/index.html @@ -8,7 +8,7 @@ Document - +
@@ -26,7 +26,7 @@ - Log in +
diff --git a/src/handler.js b/src/handler.js index 462df19..5d177d6 100644 --- a/src/handler.js +++ b/src/handler.js @@ -6,6 +6,8 @@ const { getAllPosts } = require('./queries/getalldata'); const { signup } = require('./queries/signup'); const secret = process.env.SECRET; const { checkPassword } = require('./queries/checkpassword'); +const { parse } = require('cookie'); +const { sign, verify } = require('jsonwebtoken'); const staticHandler = (response, filepath) => { const extension = filepath.split('.')[1]; @@ -29,7 +31,6 @@ const staticHandler = (response, filepath) => { }; const loginHandler = (request, response) => { - console.log(response); let data = ''; request.on('data', function (chunk) { data += chunk; @@ -43,14 +44,17 @@ const loginHandler = (request, response) => { response.writeHead(500, { 'content-type': 'text/html' }); response.end('

Failed to login the user, try again

'); }else{ - const numUsers = res; - response.writeHead(302, { 'content-type': 'text/html', 'Location': '/' }); - response.end(`

Registered ${numUsers} new users.

`); + let token = sign({'logged-in' : 'true', 'email' : `${email}`}, secret); + response.writeHead(302, { + "Content-Type": "text/html", "location": "/", 'Set-Cookie' : `token = ${token}; HttpOnly; Max-Age=9000;` + }); + response.end(); } }); }); }; + const signupHandler = (request, response) => { let data = ''; request.on('data', function (chunk) { @@ -65,9 +69,11 @@ const signupHandler = (request, response) => { response.writeHead(500, { 'content-type': 'text/html' }); response.end('

Failed to sign the user, try again

'); }else{ - const numUsers = res; - response.writeHead(302, { 'content-type': 'text/html', 'Location': '/' }); - response.end(`

Registered ${numUsers} new users.

`); + let token = sign({'logged-in' : 'true', 'email' : `${email}`}, secret); + response.writeHead(302, { + "Content-Type": "text/html", "location": "/", 'Set-Cookie' : `token = ${token}; HttpOnly; Max-Age=9000;` + }); + response.end(); } }); }); diff --git a/src/queries/checkpassword.js b/src/queries/checkpassword.js index a5922d2..4c0bf0a 100644 --- a/src/queries/checkpassword.js +++ b/src/queries/checkpassword.js @@ -6,13 +6,9 @@ const bcrypt = require('bcryptjs'); const checkPassword = (email, password, cb) => { dbConnection.query(`SELECT encrypted_password FROM users WHERE email = $1`, [email], (err, res) => { if (err) { - console.log(err); cb(err); } else { - console.log("whole res: ", res); - console.log("res rows: ", res.rows[0]); - console.log("hashed password: ", res.rows[0].encrypted_password); - console.log('password: ', password); + console.log('talking from bcrypt', res); bcrypt.compare(password, res.rows[0].encrypted_password, (err,res)=>{ if(err){ console.log('talking from bcryptcompare as res:', err) diff --git a/src/server.js b/src/server.js index 9e0dbb7..fe20aa8 100644 --- a/src/server.js +++ b/src/server.js @@ -2,7 +2,7 @@ const http = require('http'); const router = require('./router'); const server = http.createServer(router); -const port = process.env.PORT || 5000; +const port = process.env.PORT || 3000; const host = process.env.HOST || 'localhost'; server.listen(port, function() {