Create login routes
This commit is contained in:
@@ -22,7 +22,7 @@
|
|||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prestart": "npm install",
|
"prestart": "npm install",
|
||||||
"start": "nodemon index.js",
|
"start": "nodemon src/index.js",
|
||||||
"test": "mocha",
|
"test": "mocha",
|
||||||
"testandrun": "npm run prestart && npm run test && npm start"
|
"testandrun": "npm run prestart && npm run test && npm start"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ const app = express();
|
|||||||
const handlebars = require("express-handlebars");
|
const handlebars = require("express-handlebars");
|
||||||
const Handlebars = require("handlebars");
|
const Handlebars = require("handlebars");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const pgp = require("pg-promise")(); // To connect to the Postgres DB from the node server
|
|
||||||
const bodyParser = require("body-parser");
|
const bodyParser = require("body-parser");
|
||||||
const session = require("express-session"); // To set the session object. To store or access session data, use the `req.session`, which is (generally) serialized as JSON by the store.
|
const session = require("express-session"); // To set the session object. To store or access session data, use the `req.session`, which is (generally) serialized as JSON by the store.
|
||||||
const bcrypt = require("bcryptjs"); // To hash passwords
|
const bcrypt = require("bcryptjs"); // To hash passwords
|
||||||
|
|||||||
51
src/routes/login-and-registration/login-routes.js
Normal file
51
src/routes/login-and-registration/login-routes.js
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
const express = require("express");
|
||||||
|
const router = express.Router();
|
||||||
|
const bcrypt = require("bcryptjs");
|
||||||
|
const db = require("../config/database");
|
||||||
|
|
||||||
|
// Redirect to the /login endpoint
|
||||||
|
app.get("/", (req, res) => {
|
||||||
|
res.redirect("/home");
|
||||||
|
});
|
||||||
|
|
||||||
|
// Render login page for /login route
|
||||||
|
app.get("/login", (req, res) => {
|
||||||
|
res.render("/");
|
||||||
|
});
|
||||||
|
|
||||||
|
// Trigger login form to check database for matching username and password
|
||||||
|
app.post("/login", async (req, res) => {
|
||||||
|
try {
|
||||||
|
// Check if username exists in DB
|
||||||
|
const user = await db.oneOrNone(
|
||||||
|
"SELECT * FROM users WHERE username = $1",
|
||||||
|
req.body.username
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
// Redirect user to login screen if no user is found with the provided username
|
||||||
|
return res.redirect("/register");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if password from request matches with password in DB
|
||||||
|
const match = await bcrypt.compare(req.body.password, user.password);
|
||||||
|
|
||||||
|
// Check if match returns no data
|
||||||
|
if (!match) {
|
||||||
|
// Render the login page with the message parameter
|
||||||
|
return res.render("/", { message: "Password does not match" });
|
||||||
|
} else {
|
||||||
|
// Save user information in the session variable
|
||||||
|
req.session.user = user;
|
||||||
|
req.session.save();
|
||||||
|
|
||||||
|
// Redirect user to the home page
|
||||||
|
res.redirect("/");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// Direct user to login screen if no user is found with matching password
|
||||||
|
res.redirect("/register");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = loginRoutes;
|
||||||
Reference in New Issue
Block a user