Created new users database with userID, Created favoriteteams database, implemented add favorite team function but not working just yet

This commit is contained in:
Vishal Vunnam
2024-04-17 21:45:27 -06:00
parent 6faec75ae2
commit 86dbef4bf1
6 changed files with 42 additions and 23 deletions

View File

@@ -181,23 +181,35 @@ app.get('/register', (req, res) => {
// 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' });
}
// Hash the password using bcrypt library
const hash = await bcrypt.hash(req.body.password, 10);
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' });
}
// Insert username and hashed password into the 'users' table
await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]);
// Check if the username already exists in the database
const existingUser = await db.oneOrNone('SELECT * FROM users WHERE username = $1', req.body.username);
if (existingUser) {
// If a user with the same username already exists, respond with status 409 and an error message
return res.status(409).json({ status: 'error', message: 'Username already exists' });
}
// 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]);
const user = await db.oneOrNone('SELECT * FROM users WHERE username = $1', req.body.username);
req.session.user = user;
req.session.save();
console.log(user);
// Redirect user to the home page
res.redirect('/home');
} 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' });
}
});
/************************
@@ -233,9 +245,13 @@ generateClubRoutes(app);
// database configuration
app.post('/courses/add', async (req, res) => {
const { userID, teamID, teamName, teamLogo } = req.body;
try {
if (!req.session.user){
return res.status(400).json({ message: 'Login or register to add a favorite team.' });
}
const { userID, teamID, teamName, teamLogo } = req.body;
//Check if user has more than 5 teams stored
const { team_count } = await db.one(
'SELECT COUNT(*) AS team_count FROM FavoriteTeams WHERE UserID = $1',
[userID]
@@ -244,6 +260,7 @@ app.post('/courses/add', async (req, res) => {
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)',