added all testing files

This commit is contained in:
Vishal Vunnam
2024-04-10 21:53:51 -06:00
parent 7cae7b841a
commit ffc272852c
6 changed files with 2442 additions and 68 deletions

View File

@@ -56,6 +56,9 @@ app.set('views', path.join(__dirname, 'views'));
app.use(bodyParser.json()); // specify the usage of JSON for parsing request body.
// initialize session variables
app.get('/welcome', (req, res) => {
res.json({status: 'success', message: 'Welcome!'});
});
app.use(
session({
secret: process.env.SESSION_SECRET,
@@ -137,20 +140,27 @@ app.get('/', (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);
// Insert username and hashed password into the 'users' table
await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]);
//res.status(200);
res.json({status: 'success', message: 'Success'});
// 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.status(400);
//res.json({status: 'success', message: 'Invalid input'});
res.redirect('/register');
}
});
/************************
Discover Page Routes
*************************/
@@ -165,5 +175,5 @@ app.get('/', (req, res) => {
// <!-- Section 5 : Start Server-->
// *****************************************************
// starting the server and keeping the connection open to listen for more requests
app.listen(3000);
module.exports = app.listen(3000);
console.log('Server is listening on port 3000');

View File

@@ -1,4 +1,12 @@
CREATE TABLE users (
username VARCHAR(50) PRIMARY KEY,
password CHAR(60) NOT NULL
);
UserID SERIAL PRIMARY KEY,
Username VARCHAR(50) UNIQUE NOT NULL,
Password CHAR(60) NOT NULL
);
CREATE TABLE FavoriteTeam (
UserID INT REFERENCES users(UserID) ON DELETE CASCADE ON UPDATE CASCADE,
TeamID INT,
TeamName VARCHAR(50),
PRIMARY KEY (UserID, TeamID)
);