aboutsummaryrefslogtreecommitdiff
path: root/src/routes/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/index.js')
-rw-r--r--src/routes/index.js13
1 files changed, 6 insertions, 7 deletions
diff --git a/src/routes/index.js b/src/routes/index.js
index ec618c8..5c04a6e 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -1,5 +1,6 @@
1const express = require("express"); 1const express = require("express");
2const he = require("he"); 2const he = require("he");
3const bcrypt = require("bcrypt");
3const router = express.Router(); 4const router = express.Router();
4const geddit = require("../geddit.js"); 5const geddit = require("../geddit.js");
5const { db } = require("../index"); 6const { db } = require("../index");
@@ -86,9 +87,10 @@ router.post("/register", async (req, res) => {
86 return res.status(400).send("Passwords do not match"); 87 return res.status(400).send("Passwords do not match");
87 } 88 }
88 try { 89 try {
89 db.query("INSERT INTO users (username, password) VALUES (?, ?)", [ 90 const hashedPassword = await bcrypt.hash(password, 10);
91 db.query("INSERT INTO users (username, password_hash) VALUES (?, ?)", [
90 username, 92 username,
91 password, 93 hashedPassword,
92 ]).run(); 94 ]).run();
93 res.status(201).redirect("/"); 95 res.status(201).redirect("/");
94 } catch (err) { 96 } catch (err) {
@@ -101,12 +103,9 @@ router.post("/register", async (req, res) => {
101router.post("/login", async (req, res) => { 103router.post("/login", async (req, res) => {
102 const { username, password } = req.body; 104 const { username, password } = req.body;
103 const user = db 105 const user = db
104 .query("SELECT * FROM users WHERE username = ? AND password = ?", [ 106 .query("SELECT * FROM users WHERE username = ?", [username])
105 username,
106 password,
107 ])
108 .get(); 107 .get();
109 if (user) { 108 if (user && await bcrypt.compare(password, user.password_hash)) {
110 res.status(200).redirect("/"); 109 res.status(200).redirect("/");
111 } else { 110 } else {
112 res.status(401).send("Invalid credentials"); 111 res.status(401).send("Invalid credentials");