Skip to content

Commit

Permalink
function new user added to database
Browse files Browse the repository at this point in the history
Relates #16

Co-authored-by: Aishah <[email protected]>
  • Loading branch information
mhtien and aissshah committed Aug 20, 2020
1 parent 7efec46 commit 56d22d7
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 383 deletions.
2 changes: 1 addition & 1 deletion database/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DROP TABLE IF EXISTS users, posts CASCADE;
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(20) NOT NULL UNIQUE,
password VARCHAR(20) NOT NULL,
password TEXT NOT NULL,
location VARCHAR(30),
image_link TEXT
);
Expand Down
24 changes: 21 additions & 3 deletions handlers/dataSubmission.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ const path = require("path");
const bcrypt = require("bcryptjs");
const model = require("./model");

function getLoginAndSignUpHandler(request, response) {
const filePath = path.join(__dirname, "../public/login.html");
fs.readFile(filePath, (error, file) => {
if (error) {
console.error(error);
response.writeHead(404, { "content-type": "text/html" });
response.end("<h1>Not Found</h1>");
} else {
response.writeHead(200, { "content-type": "text/html" });
response.end(file);
}
});
}

function postLoginHandler() {}

function postSignUpHandler(request, response) {
Expand All @@ -18,13 +32,13 @@ function postSignUpHandler(request, response) {

bcrypt
.genSalt(12)
.then((salt) => bcrypt.hash(password, salt))
.then((salt) => bcrypt.hash(userDetails.password, salt))
.then((hash) => {
userDetails.password = hash;
model.createUser(userDetails);
})
.then(() => {
response.writeHead(200, { location: "/" });
response.writeHead(302, { location: "/" });
response.end(); // try later to see if we can add personalised message
})
.catch((error) => {
Expand Down Expand Up @@ -54,4 +68,8 @@ function getBody(request) {
});
}

module.exports = { postLoginHandler, postSignUpHandler };
module.exports = {
postLoginHandler,
postSignUpHandler,
getLoginAndSignUpHandler,
};
16 changes: 4 additions & 12 deletions handlers/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,12 @@ const add = async function (name, msgtitle, message) {
console.error(e);
}
};

const userDetails = {
username: user.get("usernamesu"),
password: user.get("passwordsu"),
location: user.get("location"),
image: user.get("imageurl"),
};

//need to include check if username already exists new checkuser function
const createUser = (userDetails) => {
return db.query(
"INSERT INTO users(username, password, location, image) VALUES($1, $2, $3, $4)", Object.values(
userDetails
)
"INSERT INTO users(username, password, location, image_link) VALUES($1, $2, $3, $4)",
Object.values(userDetails)
);
};


module.exports = { getPostsData, add, createUser };
Loading

0 comments on commit 56d22d7

Please sign in to comment.