Changes to index.js structure for middleware
This commit is contained in:
@@ -78,18 +78,30 @@ app.use(
|
||||
app.use(express.static(path.join(__dirname, 'resources')));
|
||||
|
||||
// *****************************************************
|
||||
// <!-- Section 4 : API Routes -->
|
||||
// <!-- Section 4 : Middleware -->
|
||||
// *****************************************************
|
||||
|
||||
// Importing middleware
|
||||
const fetchMatchesData = require('./resources/routes/navigation-bar/current-match-information');
|
||||
const convert_time = require('./resources/js/navigation-bar/scoreboard-header/convert-time');
|
||||
const fetchLeaguesData = require('./resources/routes/league-pages/get-current-league-information');
|
||||
|
||||
// Using middleware
|
||||
app.use(fetchMatchesData);
|
||||
app.use(convert_time);
|
||||
app.use(fetchLeaguesData);
|
||||
|
||||
// *****************************************************
|
||||
// <!-- Section 5 : API Routes -->
|
||||
// *****************************************************
|
||||
|
||||
/************************
|
||||
Header Scoreboard Routes
|
||||
League Page Routes
|
||||
*************************/
|
||||
|
||||
const fetchMatchesData = require('./resources/routes/navigation-bar/current-match-information');
|
||||
app.use(fetchMatchesData);
|
||||
|
||||
const convert_time = require('./resources/js/navigation-bar/scoreboard-header/convert-time');
|
||||
app.use(convert_time);
|
||||
// Import and call generateLeagueRoutes function
|
||||
const generateLeagueRoutes = require('./resources/routes/league-pages/generate-league-routes');
|
||||
generateLeagueRoutes(app);
|
||||
|
||||
/************************
|
||||
Login Page Routes
|
||||
@@ -98,91 +110,83 @@ app.use(convert_time);
|
||||
// Redirect to the /login endpoint
|
||||
app.get('/', (req, res) => {
|
||||
res.redirect('/home');
|
||||
});
|
||||
});
|
||||
|
||||
// Render login page for /login route
|
||||
app.get('/login', (req, res) => {
|
||||
// Render login page for /login route
|
||||
app.get('/login', (req, res) => {
|
||||
res.render('pages/home');
|
||||
});
|
||||
});
|
||||
|
||||
// Trigger login form to check database for matching username and password
|
||||
app.post('/login', async (req, res) => {
|
||||
// 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);
|
||||
|
||||
// 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');
|
||||
}
|
||||
|
||||
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 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('pages/home', { message: 'Password does not match' });
|
||||
}
|
||||
|
||||
// Check if mathc returns no data
|
||||
if (!match) {
|
||||
// Render the login page with the message parameter
|
||||
return res.render('pages/home', { message: 'Password does not match' });
|
||||
}
|
||||
// Save user information in the session variable
|
||||
req.session.user = user;
|
||||
req.session.save();
|
||||
|
||||
// Save user information in the session variable
|
||||
req.session.user = user;
|
||||
req.session.save();
|
||||
|
||||
// Redirect user to the home page
|
||||
res.redirect('/home');
|
||||
|
||||
// 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');
|
||||
// Direct user to login screen if no user is found with matching password
|
||||
res.redirect('/register');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/************************
|
||||
Registration Page Routes
|
||||
*************************/
|
||||
/************************
|
||||
Registration Page Routes
|
||||
*************************/
|
||||
|
||||
// Render registration page for /register route
|
||||
app.get('/register', (req, res) => {
|
||||
// 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 {
|
||||
if (!req.body.username || !req.body.password) {
|
||||
// If username or password is missing, respond with status 400 and an error message
|
||||
return res.status(400).json({ status: 'error', message: 'Invalid input' });
|
||||
try {
|
||||
if (!req.body.username || !req.body.password) {
|
||||
// If username or password is missing, respond with status 400 and an error message
|
||||
return res.status(400).json({ status: 'error', message: 'Invalid input' });
|
||||
}
|
||||
// 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]);
|
||||
|
||||
// Redirect user to the login page after successful registration
|
||||
res.status(200).json({ status: 'success', message: 'Registration successful' });
|
||||
} catch (error) {
|
||||
// If an error occurs during registration, respond with status 500 and an error message
|
||||
res.status(500).json({ status: 'error', message: 'An error occurred during registration' });
|
||||
}
|
||||
// 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]);
|
||||
|
||||
// Redirect user to the login page after successful registration
|
||||
res.status(200).json({ status: 'success', message: 'Registration successful' });
|
||||
} catch (error) {
|
||||
// If an error occurs during registration, respond with status 500 and an error message
|
||||
res.status(500).json({ status: 'error', message: 'An error occurred during registration' });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
/************************
|
||||
Home Page Routes
|
||||
*************************/
|
||||
|
||||
app.get('/home', (req, res) => {
|
||||
res.render('pages/home');
|
||||
res.render('pages/home');
|
||||
});
|
||||
|
||||
// Import and call generateLeagueRoutes function
|
||||
const generateLeagueRoutes = require('./resources/routes/league-pages/generate-league-routes');
|
||||
generateLeagueRoutes(app);
|
||||
|
||||
// *****************************************************
|
||||
// <!-- Section 5 : Start Server-->
|
||||
// *****************************************************
|
||||
|
||||
Reference in New Issue
Block a user