Skip to content

Commit

Permalink
merged in main
Browse files Browse the repository at this point in the history
  • Loading branch information
Harley Ewert committed Feb 29, 2024
2 parents fdc5226 + 59129bb commit 39f5bf5
Show file tree
Hide file tree
Showing 44 changed files with 3,135 additions and 184 deletions.
Binary file modified .DS_Store
Binary file not shown.
5 changes: 4 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

#Backend Options
BACKEND_PORT=3000
BACKEND_PORT=3001
BACKEND_TEST_PORT=4001
DATABASE_PORT=1111
DATABASE_TEST_PORT=2222
DATABASE_USER="pim_user"
DATABASE_PASSWORD="pim_password"
DATABASE_NAME="pim"
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pg_data
node_modules
.DS_Store
frontend/.DS_Store
.vscode
frontend/.DS_Store
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.validate": ["javascript, typescript, vue, html, css, scss, json, markdown"],
"css.lint.unknownAtRules": "ignore"
}
6 changes: 5 additions & 1 deletion backend/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#Backend Options
BACKEND_PORT=3000
BACKEND_PORT=3001
BACKEND_TEST_PORT=4001
DATABASE_PORT=1111
DATABASE_TEST_PORT=2222
DATABASE_URL=postgres://pim_user:pim_password@database:5432/pim
DATABASE_TEST_URL=postgres://pim_user:pim_password@database-test:5432/pim
JWT_SECRET=oIAea3K2KQ7xXVuyq7pmFFVVBMp86UiT
17 changes: 11 additions & 6 deletions backend/controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const db = require('../database');
// This middleware will be used to sign the token after a vendor logs in
const signToken = async (req, res, next) => {
if (res.locals.vendor === undefined) {
return res.status(401).json({message: 'Unauthorized'});
return res.status(401).json({message: 'Unauthorized: Vendor'});
}

// Sign the token with JWT_SECRET
Expand All @@ -23,17 +23,22 @@ const verifyToken = async (req, res, next) => {
// Retrieve the token from the cookie
const token = req.cookies.auth;

if (!token) {
return res.status(403).json({ message: 'No token provided' });
}

// Verify the token with JWT_SECRET
jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => {
if (err) {
res.status(401).json({message: 'Unauthorized'});
console.log(err);
res.status(401).json({message: 'Unauthorized: vendor1'});
} else {
// Update cookie data from database
db.oneOrNone('SELECT * FROM vendors WHERE vendor_id = $1', [decoded.vendor_id], (result, err) => {
if (err) {
res.status(500).json({message: 'Internal Server Error'});
} else if (result === null) {
res.status(401).json({message: 'Unauthorized'});
res.status(401).json({message: 'Unauthorized: Vendor2'});
}

res.locals.vendor = result;
Expand All @@ -50,7 +55,7 @@ const verifyToken = async (req, res, next) => {
// This middleware will be used to sign the token after an admin logs in
const signAdminToken = async (req, res, next) => {
if (res.locals.admin === undefined) {
return res.status(401).json({message: 'Unauthorized'});
return res.status(401).json({message: 'Unauthorized: Admin'});
}

// Sign the token with JWT_SECRET
Expand All @@ -69,15 +74,15 @@ const verifyAdminToken = async (req, res, next) => {
// Verify the token with JWT_SECRET
jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => {
if (err) {
return res.status(401).json({message: 'Unauthorized'});
return res.status(401).json({message: 'Unauthorized: Admin'});
} else {
// Keep the cookie up to date with the database
db.oneOrNone('SELECT * FROM admins WHERE admin_id = $1', [decoded.admin_id], (result, err) => {
if (err) {
console.log(err);
return res.status(500).json({message: 'Internal Server Error'});
} else if (result === undefined) {
return res.status(401).json({message: 'Unauthorized'});
return res.status(401).json({message: 'Unauthorized: Admin.'});
}

console.log(result);
Expand Down
19 changes: 15 additions & 4 deletions backend/controllers/VendorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ const getVendor = async (req, res, next) => {
// Middleware to verify the password of the vendor
const authenticateVendor = async (req, res, next) => {
try {

const vendor = res.locals.data;
const match = await bcrypt.compare(req.body.password, vendor.password);
const email = req.body.email;
const password = req.body.password;

if (email === undefined || password === undefined) {
res.status(401).json({message: 'Missing email or password'});
return;
}
const match = await bcrypt.compare(password, vendor.password);

// If passwords match, pass vendor object without password
if (match) {
Expand Down Expand Up @@ -97,7 +105,10 @@ const createVendor = async (req, res, next) => {
// Checks if the required fields are present
if (!password || !email || !name) {
console.log(req.body);
return res.status(400).json({error: 'Missing required fields'});
return res.status(400).json({
error: 'Missing required fields',
data: req.body,
});
}

// Hashes the password using bcrypt
Expand All @@ -107,7 +118,7 @@ const createVendor = async (req, res, next) => {
passwordHash = await hash(password, salt);
} catch (err) {
console.log(err);
res.status(495).json({error: err});
res.status(495).json({error: "Error hashing password"});
return;
}

Expand All @@ -134,7 +145,7 @@ const createVendor = async (req, res, next) => {

// Other internal error
console.log(err);
res.status(500).json({error: err});
res.status(500).json({error: "Internal Server Error"});
return;
}

Expand Down
20 changes: 16 additions & 4 deletions backend/database.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
// Import the pg-promise library and initialize it
const pgp = require('pg-promise')();

// Connect to the database using the environment variable DATABASE_URL
if (process.env.DATABASE_URL === undefined) {
throw new Error('DATABASE_URL environment variable not set');
let db;

if (process.env.NODE_ENV === undefined) {
throw new Error('NODE_ENV environment variable not set');
}
if (process.env.DATABASE_URL === undefined || process.env.DATABASE_TEST_URL === undefined) {
throw new Error('DATABASE_URL or DATABASE_TEST_URL environment variable not set');
}

const db = pgp(process.env.DATABASE_URL);
// Connect to the database using the environment variable DATABASE_URL
if (process.env.NODE_ENV === 'test') {
console.log('Connecting to database using url: ' + process.env.DATABASE_TEST_URL);
db = pgp(process.env.DATABASE_TEST_URL);
}
else {
console.log('Connecting to database using url: ' + process.env.DATABASE_URL);
db = pgp(process.env.DATABASE_URL);
}

/*
The database object above represents a connection to our database. However,
Expand Down
37 changes: 34 additions & 3 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,44 @@ app.get('/', (req, res) => {
res.status(202).send('Hello World!');
});

app.get('/api/health', (req, res) => {
db.one('SELECT 1 AS value')
.then(() => {
res.status(200).json({
status: 'success',
message: 'Database connection is healthy',
});
})
.catch(error => {
console.error('Database connection error:', error);
res.status(503).json({
status: 'error',
message: 'Database connection is unhealthy',
error: error.message,
});
});
});



/*
The backend should be listening on port 3000 within its container,
but the container's port 3000 is mapped externally to 3001.
TL;DR the backend is running on port 3001 on the host machine.
*/

app.listen(process.env.BACKEND_PORT, () => {
console.log(`PIM backend app listening on port ${process.env.BACKEND_PORT}`);
});
if (process.env.NODE_ENV === 'test') {
app.listen(process.env.BACKEND_TEST_PORT, () => {
console.log(`PIM backend app listening on port ${process.env.BACKEND_TEST_PORT}`);
});
// export app to import into test files
module.exports = app;
return;
}
else if (process.env.NODE_ENV === 'dev') {
app.listen( process.env.BACKEND_PORT, () => {
console.log(`PIM backend app listening on port ${process.env.BACKEND_PORT}`);
});
return;
}
49 changes: 49 additions & 0 deletions docker-compose-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
version: "3"
services:
backend-test:
image: node:alpine3.17
container_name: backend-test
working_dir: /app
command: npm run dev
ports:
- "${BACKEND_TEST_PORT}:4000"
volumes:
- ./backend:/app
environment:
- NODE_ENV=test
depends_on:
database-test:
condition: service_healthy

# PostgreSQL
database-test:
hostname: database-test
image: postgres:latest
container_name: postgres-test
ports:
- "${DATABASE_TEST_PORT}:5432"
environment:
POSTGRES_DB: ${DATABASE_NAME}
POSTGRES_USER: ${DATABASE_USER}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
volumes:
- ./sql-scripts:/docker-entrypoint-initdb.d # Mount directory for initial SQL files


# React.js Frontend
frontend-test:
image: node:alpine3.17
container_name: frontend-test
working_dir: /app
command: npm run test
volumes:
- ./frontend:/app
ports:
- "4000:4000"
depends_on:
- backend-test
6 changes: 3 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ services:
working_dir: /app
command: npm run dev
ports:
- "3001:3000"
- "${BACKEND_PORT}:3001"
volumes:
- ./backend:/app
environment:
- NODE_ENV=development
- NODE_ENV=dev
depends_on:
- database

Expand All @@ -21,7 +21,7 @@ services:
image: postgres:latest
container_name: postgres
ports:
- "5432:5432"
- "${DATABASE_PORT}:5432"
environment:
POSTGRES_DB: ${DATABASE_NAME}
POSTGRES_USER: ${DATABASE_USER}
Expand Down
File renamed without changes.
Loading

0 comments on commit 39f5bf5

Please sign in to comment.