Skip to content

Commit

Permalink
log in validation
Browse files Browse the repository at this point in the history
Relates #33 #34 #29 #25 #24 #18
  • Loading branch information
developess committed Aug 24, 2018
1 parent dd922dc commit 328ab4d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
10 changes: 10 additions & 0 deletions src/controllers/formError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
exports.get = (req, res) => {
const { type } = req.params;
let errorMsg = "";
if (type == 'pw') {
errorMsg = "Password incorrect"
} else if (type == 'un') {
errorMsg = "Username not found. Please note usernames are case sensitive"
}
res.render("formError", { message: errorMsg });
};
15 changes: 12 additions & 3 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const addArticle = require("./add-article");
const signUp = require("./sign-up");
const logIn = require("./log-in");
const checkname = require("./checkname");
const formError = require("./formError");

const queries = require("../model/index");

Expand All @@ -20,6 +21,7 @@ router.get("/add-article", requiresLogin, addArticle.get);
router.get("/sign-up", signUp.get);
router.get("/username/:name", checkname.get);
router.get("/log-in", logIn.get);
router.get("/formError/:type", formError.get);
router.post("/add-article/new", (req, res) => {
// console.log('req: ', req.body)
queries.addArticle(req.body);
Expand All @@ -32,17 +34,24 @@ router.post("/add-user", (req, res) => {
});
router.post("/login-check", (req, res) => {
queries
.doesUserExist(req.body)
.checkPassword(req.body)
.then(resolve => {
if (resolve) {
// if password valid, set session to logged in and redirect
req.session.loggedIn = true;
res.redirect(302, "/");
} else {
res.redirect(302, "/log-in");
res.redirect(302, "/formError/pw");
}
})
.catch(e => console.log(e));
.catch(e => {
if (e == "user not found") {
res.redirect(302, "/formError/un");
// res.send("username not found")
} else {
console.log(e)
}
});
});
router.get("/log-out", (req, res) => {
req.session.loggedIn = false;
Expand Down
2 changes: 1 addition & 1 deletion src/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
getArticleDetail: getArticleDetail,
getArticles: getArticles,
getTopics: getTopics,
doesUserExist: loginCheck,
checkPassword: loginCheck,
addArticle: addArticle,
getName: getName
};
42 changes: 23 additions & 19 deletions src/model/loginCheck.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
const db = require('../../db/db_connection');
const bcrypt = require('bcryptjs');
const db = require("../../db/db_connection");
const bcrypt = require("bcryptjs");
const checkUser = object => {
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
const name = object.name;
const password = object.password;
db.query(`SELECT password FROM users WHERE name = '${name}';`)
.then (res => {
const hash = res[0].password;
bcrypt.compare(password, hash, (err, res) => {
if (err) reject(`error validating password: ${err}`);
else {
console.log('password is valid?: ', res);
resolve(res);
}
})
})
.catch(e => reject(`Couldn't update db: ${e}`));
})
}
.then(res => {
if (res.length < 1) {
reject("user not found");
} else {
const hash = res[0].password;
bcrypt.compare(password, hash, (err, res) => {
if (err) reject(`error hashing pw: ${err}`);
else {
// console.log("password is valid?: ", res);
resolve(res);
}
});
}
})
.catch(e => reject(`Couldn't update db: ${e}`));
});
};

module.exports = checkUser;
// Log in:
// get user data where name = username inputted
// compare hashed input to hashed password
// return true/false?
// get user data where name = username inputted
// compare hashed input to hashed password
// return true/false?
4 changes: 4 additions & 0 deletions src/views/formError.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<form>
<h2>{{message}}</h2>
<a href="/log-in"><button type="button">Try Again</button><a>
</form>

0 comments on commit 328ab4d

Please sign in to comment.