Skip to content

Commit

Permalink
first sessionID functionality - on registration > relates #20
Browse files Browse the repository at this point in the history
  • Loading branch information
VirtualDOMinic committed Aug 16, 2018
1 parent 94c3797 commit d104c36
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/database/db_build.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BEGIN;

DROP TABLE IF EXISTS users, items, loans CASCADE;
DROP TABLE IF EXISTS users, items, loans, active_sessions CASCADE;

CREATE TABLE users (
id SERIAL PRIMARY KEY,
Expand Down Expand Up @@ -48,4 +48,13 @@ INSERT INTO loans (item_id, borrowers_id, issue_date, return_date) VALUES
(4, 1, '2018-08-05', '2018-08-06'),
(4, 1, '2018-08-07', NULL);

CREATE TABLE active_sessions (
id SERIAL PRIMARY KEY,
session_id VARCHAR(100) NOT NULL UNIQUE,
email VARCHAR(80) REFERENCES users(email) NOT NULL,
creation_date DATE NOT NULL DEFAULT CURRENT_DATE
);

-- NEED TO INSERT DUMMY DATA INTO sessions TABLE

COMMIT;
39 changes: 35 additions & 4 deletions src/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const { postData, checkUser, insertData } = require("./queries/postData");
const getData = require("./queries/getData");
const runDbBuild = require("./database/db_build");
const passwords = require("./passwords");
const querystring = require("querystring")
const querystring = require("querystring");
const crypto = require("crypto");

const buildPath = function(myPath) {
return path.join(__dirname, "..", "public", myPath);
Expand All @@ -21,6 +22,18 @@ const contentType = {
".gif": "image/gif"
};

const sessionIDGen = function(length = 24){
return new Promise((resolve, reject) => {
crypto.randomBytes(48, (err, buffer) => {
if (err) {
reject(err);
} else {
resolve (buffer.toString("hex"))
}
})
})
}

const handlers = {
collectData(req, cb) {
let data = "";
Expand Down Expand Up @@ -98,14 +111,14 @@ const handlers = {
if (err) {
res.writeHead(500, { "Content-Type": "text/html" });
res.end("<h1>Server Error</h1>");
} else if (res) {
} else if (result) {
res.writeHead(200, { "Content-Type": "text/html" });
res.end("<h1>Email already exists</h1>");
} else {
passwords.hashPassword(password, (err, hashedPassword) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/html" });
res.end("<h1>Server Error</h1>");
res.end("<h1>Hashed pw error</h1>");
}
passwords.storePassword(
name,
Expand All @@ -115,8 +128,26 @@ const handlers = {
(err, result) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/html" });
res.end("<h1>Server Error</h1>");
res.end("<h1>Server Error in storepassword func</h1>");
}

// Create session token!
sessionIDGen()
.then(
sessionID => {
passwords.storeSession(
sessionID,
email,
(err, result) => {
console.log("Store Session func reached")
if (err) {
res.writeHead(500, { "Content-Type": "text/html" });
res.end("<h1>Server Error in storeSession func</h1>");
}
}
)}
)

// create cookie
// store session data
// all that jazz
Expand Down
14 changes: 14 additions & 0 deletions src/passwords.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,25 @@ const passwords = {
dbConnection.query(
`INSERT INTO users (name, email, fav_colour, password_hash) VALUES ($1, $2, $3, $4)`,
[name, email, favColour, hashedPassword],
// `INSERT INTO users (name, email, fav_colour, password_hash) VALUES ('${name}', '${email}', '${favColour}', '${hashedPassword}')`,
// `INSERT INTO users (name, email, fav_colour, password_hash) VALUES ('Joe', '[email protected]', '0947dd', '$2b$12$9KMMDuR2Le5n1.tl1LYqOuVCRXjwpIRfj0RafQa/mppqgNTD7.P8u')`,
(err, res) => {
if (err) return cb(err);
return cb(null, res);
}
);
},

storeSession: (sessionID, email, cb) => {
console.log("session ID: ", sessionID)
dbConnection.query(
`INSERT INTO active_sessions (session_id, email) VALUES ($1, $2)`,
[sessionID, email],
(err, res) => {
if(err) return cb(err);
return cb(null, res);
}
)
}

// takes in hash of password, compares against hash from database
Expand Down
21 changes: 21 additions & 0 deletions testingcrypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const crypto = require("crypto");


function random(length = 24){
return new Promise((resolve, reject) => {
crypto.randomBytes(48, (err, buffer) => {
if (err) {
reject(err);
} else {
resolve (buffer.toString("hex"))
}
})
})
}




var sessionID = random()
.then(dsdsfd => console.log(dsdsfd))

0 comments on commit d104c36

Please sign in to comment.