Skip to content

Commit

Permalink
sets cookies on login and sign up
Browse files Browse the repository at this point in the history
Relates #6
  • Loading branch information
missKatiaPunter committed Apr 12, 2018
1 parent b01d9d9 commit 4149691
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
4 changes: 2 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<title>Document</title>
</head>
<body>
<dialog id="signup" role="dialog" open>
<dialog id="signup" role="dialog">
<form id="signup-form" action='/signup' method='post'>
<label for="email">Email:</label>
<input id="email" name="email" type="email" required/>
Expand All @@ -26,7 +26,7 @@
<input id="login-email" name="email" required/>
<label for="login-password">Password:</label>
<input id="login-password" name="password" type="password" required/>
<input type="submit" id="login-btn">Log in</input>
<button type="submit" id="login-btn">Log in</button>
</form>
<button id="new-user">Click to Sign up!</button>
</dialog>
Expand Down
20 changes: 13 additions & 7 deletions src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -29,7 +31,6 @@ const staticHandler = (response, filepath) => {
};

const loginHandler = (request, response) => {
console.log(response);
let data = '';
request.on('data', function (chunk) {
data += chunk;
Expand All @@ -43,14 +44,17 @@ const loginHandler = (request, response) => {
response.writeHead(500, { 'content-type': 'text/html' });
response.end('<h1>Failed to login the user, try again</h1>');
}else{
const numUsers = res;
response.writeHead(302, { 'content-type': 'text/html', 'Location': '/' });
response.end(`<h1>Registered ${numUsers} new users.</h1>`);
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) {
Expand All @@ -65,9 +69,11 @@ const signupHandler = (request, response) => {
response.writeHead(500, { 'content-type': 'text/html' });
response.end('<h1>Failed to sign the user, try again</h1>');
}else{
const numUsers = res;
response.writeHead(302, { 'content-type': 'text/html', 'Location': '/' });
response.end(`<h1>Registered ${numUsers} new users.</h1>`);
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();
}
});
});
Expand Down
6 changes: 1 addition & 5 deletions src/queries/checkpassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 4149691

Please sign in to comment.