CSS hookups added and docker-compose compatibility changes made
This commit is contained in:
@@ -27,7 +27,7 @@ const hbs = handlebars.create({
|
||||
// database configuration
|
||||
const dbConfig = {
|
||||
host: 'db', // the database server
|
||||
port: 5433, // the database port
|
||||
port: 5432, // the database port
|
||||
database: process.env.POSTGRES_DB, // the database name
|
||||
user: process.env.POSTGRES_USER, // the user account to connect with
|
||||
password: process.env.POSTGRES_PASSWORD, // the password of the user account
|
||||
@@ -71,13 +71,94 @@ app.use(
|
||||
);
|
||||
|
||||
// Serve static files from the 'public' directory
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
app.use(express.static(path.join(__dirname, 'resources')));
|
||||
|
||||
// *****************************************************
|
||||
// <!-- Section 4 : API Routes -->
|
||||
// *****************************************************
|
||||
|
||||
// TODO - Include your API routes here
|
||||
/************************
|
||||
Login Page Routes
|
||||
*************************/
|
||||
|
||||
// Redirect to the /login endpoint
|
||||
app.get('/', (req, res) => {
|
||||
res.redirect('/login');
|
||||
});
|
||||
|
||||
// Render login page for /login route
|
||||
app.get('/login', (req, res) => {
|
||||
res.render('pages/login');
|
||||
});
|
||||
|
||||
// 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 mathc returns no data
|
||||
if (!match) {
|
||||
// Render the login page with the message parameter
|
||||
return res.render('pages/login', { message: 'Password does not match' });
|
||||
}
|
||||
|
||||
// Save user information in the session variable
|
||||
req.session.user = user;
|
||||
req.session.save();
|
||||
|
||||
// Redirect user to the home page
|
||||
res.redirect('/home');
|
||||
|
||||
} catch (error) {
|
||||
// Direct user to login screen if no user is found with matching password
|
||||
res.redirect('/register');
|
||||
}
|
||||
});
|
||||
|
||||
/************************
|
||||
Registration Page Routes
|
||||
*************************/
|
||||
|
||||
// Render registration page for /register route
|
||||
app.get('/register', (req, res) => {
|
||||
res.render('pages/register');
|
||||
});
|
||||
|
||||
// Trigger Registration Form to Post
|
||||
app.post('/register', async (req, res) => {
|
||||
try {
|
||||
// Hash the password using bcrypt library
|
||||
const hash = await bcrypt.hash(req.body.password, 10);
|
||||
|
||||
// Insert username and hashed password into the 'users' table
|
||||
await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]);
|
||||
|
||||
// Direct user to login screen after data has been inserted successfully
|
||||
res.redirect('/login');
|
||||
} catch (error) {
|
||||
// If the insert fails, redirect to GET /register route
|
||||
res.redirect('/register');
|
||||
}
|
||||
});
|
||||
|
||||
/************************
|
||||
Discover Page Routes
|
||||
*************************/
|
||||
|
||||
// Render registration page for /register route
|
||||
app.get('/home', (req, res) => {
|
||||
res.render('pages/home');
|
||||
});
|
||||
|
||||
|
||||
// *****************************************************
|
||||
|
||||
Reference in New Issue
Block a user