added js and worked on Login feature(still in progress)

This commit is contained in:
Vishal Vunnam
2024-04-17 18:06:45 -06:00
parent 77b990cd1e
commit 6faec75ae2
15 changed files with 99 additions and 23 deletions

View File

@@ -155,13 +155,15 @@ app.post('/login', async (req, res) => {
// Render the login page with the message parameter
return res.render('pages/home', { message: 'Password does not match' });
}
else{
// Save user information in the session variable
req.session.user = user;
req.session.save();
console.log(user);
// 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');
@@ -203,7 +205,8 @@ app.post('/register', async (req, res) => {
*************************/
app.get('/home', (req, res) => {
res.render('pages/home');
const loggedIn = req.session.user ? true : false;
res.render('pages/home', { loggedIn: loggedIn, user: req.session.user});
});
/************************
@@ -222,6 +225,40 @@ generateLeagueRoutes(app);
const generateClubRoutes = require('./resources/routes/club-pages/generate-club-routes');
generateClubRoutes(app);
/************************
Favorite Team Database
*************************/
// Function to add a new row to the FavoriteTeams table
// database configuration
app.post('/courses/add', async (req, res) => {
const { userID, teamID, teamName, teamLogo } = req.body;
try {
const { team_count } = await db.one(
'SELECT COUNT(*) AS team_count FROM FavoriteTeams WHERE UserID = $1',
[userID]
);
if (team_count >= 5) {
console.log('User has reached the maximum number of favorite teams.');
return res.status(400).json({ message: 'User has reached the maximum number of favorite teams.' });
}
const query = {
text: 'INSERT INTO FavoriteTeams (UserID, TeamID, TeamName, TeamLogo) VALUES ($1, $2, $3, $4)',
values: [userID, teamID, teamName, teamLogo],
};
await db.none(query);
console.log('New favorite team added successfully.');
return res.status(200).json({ message: 'New favorite team added successfully.' });
} catch (error) {
console.error('Error adding favorite team:', error);
return res.status(500).json({ error: 'Error adding favorite team' });
}
});
// *****************************************************
// <!-- Section 5 : Start Server-->
// *****************************************************