Skip to content

Commit

Permalink
Petition Project, Register and Log In
Browse files Browse the repository at this point in the history
  • Loading branch information
LinaLtu committed Feb 23, 2018
1 parent e09684f commit 355a560
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 5 deletions.
21 changes: 21 additions & 0 deletions config/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,28 @@ function countSignatures() {
.catch(err => console.log(err));
}

function insertRegistrationInfo(first, last, email, password) {
const q = `INSERT INTO users (first, last, email, password) VALUES ($1, $2, $3, $4) RETURNING id`;
const params = [first, last, email, password];

return db
.query(q, params)
.then(registrationResults => {
console.log("Insert new signature was successful");
return registrationResults;
})
.catch(err => console.log(err));
}

function getPasswordFromDB(email) {
const q = `SELECT * FROM users WHERE email = $1`;
const param = [email];
return db.query(q, param);
}

module.exports.insertSignatures = insertSignatures;
module.exports.getSignature = getSignature;
module.exports.getSignedNames = getSignedNames;
module.exports.countSignatures = countSignatures;
module.exports.insertRegistrationInfo = insertRegistrationInfo;
module.exports.getPasswordFromDB = getPasswordFromDB;
134 changes: 133 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,48 @@ const bodyParser = require("body-parser");
const hb = require("express-handlebars");
const cookieSession = require("cookie-session");
const db = require("./config/db.js");
const bcrypt = require("bcryptjs");
const insertSignatures = db.insertSignatures;
const getSignature = db.getSignature;
const getSignedNames = db.getSignedNames;
const countSignatures = db.countSignatures;
const insertRegistrationInfo = db.insertRegistrationInfo;
const getPasswordFromDB = db.getPasswordFromDB;

var id;
var userId;

function hashPassword(plainTextPassword) {
return new Promise(function(resolve, reject) {
bcrypt.genSalt(function(err, salt) {
if (err) {
return reject(err);
}
bcrypt.hash(plainTextPassword, salt, function(err, hash) {
if (err) {
return reject(err);
}
resolve(hash);
});
});
});
}

function checkPassword(textEnteredInLoginForm, hashedPasswordFromDatabase) {
return new Promise(function(resolve, reject) {
bcrypt.compare(
textEnteredInLoginForm,
hashedPasswordFromDatabase,
function(err, doesMatch) {
if (err) {
reject(err);
} else {
resolve(doesMatch);
}
}
);
});
}

app.use(bodyParser.urlencoded({ extended: false }));

Expand Down Expand Up @@ -60,7 +98,7 @@ app.post("/", (req, res) => {

app.get("/thankyou", (req, res) => {
if (req.session.signatureId) {
console.log(req.session.signatureId);
//console.log(req.session.signatureId);
getSignature(req.session.signatureId).then(idResults => {
res.render("thankyou", {
layout: "main",
Expand All @@ -70,6 +108,94 @@ app.get("/thankyou", (req, res) => {
}
});

app.post("/register", (req, res) => {
if (
req.body.first &&
req.body.last &&
req.body.email &&
req.body.password
) {
hashPassword(req.body.password)
.then(hash =>
insertRegistrationInfo(
req.body.first,
req.body.last,
req.body.email,
hash
).then(insertRegistrationInfo => {
id = insertRegistrationInfo.rows[0].id;
req.session.id = id;
console.log(
"This is your id: " + insertRegistrationInfo.rows[0].id
);
console.log("You've registered");
res.redirect("/");
})
)
.catch(() => {
res.render("register", {
error: true
});
});
} else {
res.render("register", {
layout: "main",
error: true
});
}
});

app.get("/login", (req, res) => {
if (userId) {
res.redirect("/");
} else {
res.render("login", {
layout: "main"
});
}
});

app.post("/login", (req, res) => {
if (req.body.email && req.body.password) {
getPasswordFromDB(req.body.email)
.then(hashedPassword =>
checkPassword(
req.body.password,
hashedPassword.rows[0].password
).then(value => {
if (value === true) {
userId = hashedPassword.rows[0].id;
let first = hashedPassword.rows[0].first;
let last = hashedPassword.rows[0].last;

req.session.userId = userId;
res.render("welcome", {
layout: "main"
});
} else {
console.log("We are here");
res.render("login", {
error: true
});
}
//check if it correct, set the session and redirect, otherwise render an error
})
)
.catch(err => {
console.log("We are here 2", err);
res.render("login", {
error: true
});
});
} else {
console.log("We are here 3");
res.render("login", {
layout: "main",
error: true
});
}
});

app.get("/signed", (req, res) => {
getSignedNames().then(signedNames => {
console.log(signedNames);
Expand All @@ -80,6 +206,12 @@ app.get("/signed", (req, res) => {
});
});

app.get("/register", (req, res) => {
res.render("register", {
layout: "main"
});
});

app.listen(8080, () => {
console.log("Listening Petition");
});
6 changes: 6 additions & 0 deletions public/styles/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ h3 {

.picture {
text-align: center;
margin: 20px;
}

.picture-thanks,
Expand Down Expand Up @@ -81,3 +82,8 @@ h3 {
font-weight: bold;
border: 1px dashed grey;
}

.input-field {
width: 600px;
padding: 10px;
}
3 changes: 3 additions & 0 deletions sql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ DROP TABLE IF EXISTS signatures;

CREATE TABLE signatures (
id SERIAL PRIMARY KEY,
-- user_id INTEGER NOT NULL,
-- a foreign key
first VARCHAR(200) NOT NULL,
last VARCHAR(200) NOT NULL,
signature TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

9 changes: 9 additions & 0 deletions users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

CREATE TABLE users (
id SERIAL PRIMARY KEY,
first VARCHAR (255) NOT NULL,
last VARCHAR (255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR (100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
22 changes: 22 additions & 0 deletions views/login.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div class="main">

<div class="picture">
<img src="/pizza.jpg" id="ananas">
</div>

<h1>Please log in</h1>

<div class="form">
<form method="post">
<input type="text" name="email" value="" placeholder="Email Address" class="input-field"><br/><br/>
<input type="text" name="password" value="" placeholder="Password" class="input-field"><br/><br/>
<br><br>
{{#error}}
<div class="error">Oooops! Something went wrong. Try again</div>
{{/error}}
<input type="submit" value="Log In" id="submit-btn">
</form>
</div>


</div>
25 changes: 25 additions & 0 deletions views/register.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div class="main">
<h1>Register for our good cause</h1>

<div class="picture">
<img src="/pizza.jpg" id="ananas">
</div>

<div class="form">
<form method="post">

<input type="text" name="first" value="" placeholder="First Name" class="input-field"><br/><br/>
<input type="text" name="last" value="" placeholder="Last Name" class="input-field"><br/><br/>
<input type="text" name="email" value="" placeholder="Email Address" class="input-field"><br/><br/>
<input type="text" name="password" value="" placeholder="Password" class="input-field"><br/><br/>
<br>
{{#error}}
<div class="error">Oooops! Something went wrong. Try again</div>
{{/error}}
<br>
<input type="submit" value="Sign Up" id="submit-btn">
</form>
</div>


</div>
6 changes: 2 additions & 4 deletions views/welcome.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

<div class="form">
<form method="post">
First name:
<input type="text" name="first" value="">
Last name:
<input type="text" name="last" value="">
<input type="text" name="first" value="" placeholder="First Name" class="input-field"><br/><br/>
<input type="text" name="last" value="" placeholder="Last Name" class="input-field">
<br><br>
<input type="hidden" name="signature" value="">
{{#error}}
Expand Down

0 comments on commit 355a560

Please sign in to comment.