Skip to content

Commit

Permalink
Merge pull request #5 from TeaghanEveleigh/master
Browse files Browse the repository at this point in the history
Refactored: altered endpoints
  • Loading branch information
TeaghanEveleigh authored Aug 7, 2024
2 parents 6f69547 + 5c786ba commit 64870df
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 71 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.env
.env
.crt
84 changes: 84 additions & 0 deletions backend/Documentation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@



please note that as it stands the application runs on local host port 4000 so for any endpoints must start with the following

REQUIRES VERIFICATION : if the user is not logged in and has a verified token they can not use this endpoint
the frontend should first login/signup and get the token and only then make requests

http://localhost:4000

Endpoints for application


USER Endpoints : for this the convention is /user/{the desired endpoint}


http://localhost:4000/user


Post Requests :

Login :

- logs the user in and returns verification token in the response , this token is used for validation

endpoint - http://localhost:4000/user/login

Request body - [email , password ]


Signup :

- signs user in creating an account if account already exists it sends back unsuccesfull along with the message
and returns verification token in the response , this token is used for validation

endpoint - http://localhost:4000/user/signup

Request body - [email , password ]


Get Requests :


REQUIRES VERIFICATION

Balance :

endpoint - http://localhost:4000/user/balance

-Balance gets the users balance but please note this is an endpoint that REQUIRES VERIFICATION so if the user is not logged in and has a verified token they can not use this endpoint
the frontend should first login/signup and get the token and only then make requests



http://localhost:4000/transactions


REQUIRES VERIFICATION

Post requests :

endpoint - http://localhost:4000/transactions/makeTransaction

Request body - [ amount,title,description ]

-the endpoint makes a transaction and adds it to the table of transactions it assigns it to our user , it automatically updates the specified users balance to account for the new transaction





REQUIRES VERIFICATION

Get requests :

endpoint - http://localhost:4000/transactions/getTransactionByPage

Params : pagenumber = x

- for this you just need to in the request params assign a value pagenumber , this will return the users transactions. It bassicly just works in the way you would expect
if you do the pagenumber =1 you get the first ten transactions // frontend peeps please let me know if you want to change the number of requests



25 changes: 25 additions & 0 deletions backend/src/certs/ca.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-----BEGIN CERTIFICATE-----
MIIEQTCCAqmgAwIBAgIUKo2BX+ykzxZslXAlxKniKTUO1Z0wDQYJKoZIhvcNAQEM
BQAwOjE4MDYGA1UEAwwvZjdlNjkyMmYtYjc1Mi00ZWQzLWJhMjUtZTI1YjAwNjU0
YTE3IFByb2plY3QgQ0EwHhcNMjQwODA0MjMzMjU0WhcNMzQwODAyMjMzMjU0WjA6
MTgwNgYDVQQDDC9mN2U2OTIyZi1iNzUyLTRlZDMtYmEyNS1lMjViMDA2NTRhMTcg
UHJvamVjdCBDQTCCAaIwDQYJKoZIhvcNAQEBBQADggGPADCCAYoCggGBAKSXRMAi
unkpsnRzCFavjmxpRNj+amFYjvem6rQIWe+YLtqw24Mk326iWv6fVCgESV0APWnM
vHp4HawWCqnpt3KVf6xTmAfNAbazODJZZTg3HHKidQSU4z/7cUUJdrneskjthYAD
4IxqE555PIWDMoFwq1/W14DihLRmuzdDOCaxH4EKfhSOjzxwyM/tQjVvauhpuAFy
TgnmBp8scue96F6hKTBccgBzA4xBT8lcj5KvCjLY6Fn2lQCU+Z0KPtqC97OWnu0l
/uiAzK6Ciu0hHRNQ+vo+Epj+Xeg+mcerg4TuZlJWCFatEfPmwBcpzXkkmkrUI/Qu
pjvlkYLX/R2MWTH/oG4zLbmpPQHVF8CXDxvfCpwcJCbO96VAy4sHKfwaX96Rbtge
urrFtvYQSI8REEnkIT/3zt9PxusL/caqNX0roW79qhCJ4D0BMaSMk1gkNl1fomJA
l1h7HsL+UGiYxOCyppmuVuiu3gtle76RGz/ZaKbCOiK0/GkxEa/8eTVBLwIDAQAB
oz8wPTAdBgNVHQ4EFgQUCboAowy7OHOkydNbBfuQ/iH23cUwDwYDVR0TBAgwBgEB
/wIBADALBgNVHQ8EBAMCAQYwDQYJKoZIhvcNAQEMBQADggGBAHmiS7RSqL8JaH9L
vbASqpSsFHTXCNkcy5qXbC12+srSX2NKvXDoA0Bp85kWnupuk4rmR6J7CQNl2Utk
hCaCxl5IpJ6IjqEdxoemnRrGRtGwHA68LUj3PofbWklxSgzkxXTkY1gKnTnUomj+
PLyM2B4afe3yngB9gDxvePfFdu2K9ij1qA9UJL3ffuDSsStE4o0f/qHDf7i3L+9g
JDPx+fkxnv0Eco5vsPh9MigsMQvlfYLgeg69lcsI5jyqf6SKElaQaIF05PrqFk5L
cbWKnZYeNPH32DwowVHeTjzbKLybq4nMzhyhBbXkIkHgN2ib5cmUm6lEjyUUbKgG
ZoVF2/mYNB/Z/VobXDbWXZNHKAnoPISz2nVnGhMXw2CArBNd8fYmj+khe4IeVrcz
2Haa/SEbiknxymB3w2yIsMZMcv4PpPZ7OsZq98/IYmg6a7eEg+kyMVSi+a9O0syy
nxM1F7OXJD5zfups8JOOPLrlApyKmVdv/9iUq6DRn5V526+Jtg==
-----END CERTIFICATE-----
18 changes: 18 additions & 0 deletions backend/src/config/corsConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const cors = require('cors');

const allowedOrigins = ['http://localhost:4000']; // Add your allowed origins here

const corsOptions = {
origin: function (origin, callback) {
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
};

module.exports = cors(corsOptions);
1 change: 1 addition & 0 deletions backend/src/config/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const fs = require('fs');
require('dotenv').config(); // Load environment variables from .env file


const caCertPath = path.resolve(__dirname, process.env.CA_CERT_PATH);
const pool = new Pool({
user: process.env.PG_USER,
Expand Down
27 changes: 27 additions & 0 deletions backend/src/controllers/transactionsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

const {getUserTransactionsByPage , makeTransaction} = require('../services/transactionService')
const jwt = require('jsonwebtoken');

exports.transaction = async (req, res) => {
const {amount,title,description} = req.body;
const userID = req.user.id;
try {
await makeTransaction(userID , amount , title , description );
res.send({ success: true });
} catch (error) {
console.error('Error making transactions', error);
res.status(500).send({ success: false, error: error.message });
}
}

exports.transactions = async(req , res) =>{
const {pageNumber} = req.params;
const userID = req.user.id;
try{
const result = await getUserTransactionsByPage(userID , pageNumber);
res.status(200).send({sucess : true , result : result})
}catch{
console.error('Error when getting transactions' , error);
res.status(500).send({ success: false, error: error.message });
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
const express = require('express');
const { checkEmailExists, checkPasswordCorrect, createUser , getUserID, makeTransaction , getBalance, getUserTransactionsByPage} = require('../services/userService');
const {isAuthenticated} = require('../middleware/autMiddleware')
const router = express.Router();
const jwt = require('jsonwebtoken');

router.post('/checkEmailExists', async (req, res) => {
const email = req.body.email;
const emailExists = await checkEmailExists(email);
res.send({ emailExists });
});

router.post('/checkPasswordCorrect', async (req, res) => {
const email = req.body.email;
const password = req.body.password;
const passwordCorrect = await checkPasswordCorrect(email, password);
res.send({ passwordCorrect });
});
const { checkEmailExists, checkPasswordCorrect, createUser , getUserID, getBalance} = require('../services/userService');
const jwt = require('jsonwebtoken');

router.post('/login', async (req, res) => {
exports.login = async (req, res) => {
const { email, password } = req.body;

try {
Expand All @@ -42,9 +27,9 @@ router.post('/login', async (req, res) => {
console.error('Error in login:', error);
res.status(500).send({ success: false, error: error.message });
}
});

router.post('/signup', async (req, res) => {
}
exports.signup = async (req, res) => {
const { email, password } = req.body;

try {
Expand All @@ -71,8 +56,9 @@ router.post('/signup', async (req, res) => {
console.error('Error in signup:', error);
res.status(500).send({ success: false, error: error.message });
}
});
router.get('/getBalance', isAuthenticated, async (req, res) => {
}

exports.balance = async (req, res) => {
const userID = req.user.id;
try {
const result = await getBalance(userID);
Expand All @@ -81,29 +67,4 @@ router.get('/getBalance', isAuthenticated, async (req, res) => {
console.error('Error getting balance:', error);
res.status(500).send({ success: false, error: error.message });
}
});
router.post('/makeTransaction', isAuthenticated, async (req, res) => {
const {amount,title,description} = req.body;

const userID = req.user.id;
try {
await makeTransaction(userID , amount , title , description );
res.send({ success: true });
} catch (error) {
console.error('Error making transactions', error);
res.status(500).send({ success: false, error: error.message });
}
});
router.post('/getTransactionByPage' , isAuthenticated , async(req , res) =>{
const {pageNumber} = req.body;
const userID = req.user.id;
try{
const result = await getUserTransactionsByPage(userID , pageNumber);
res.status(200).send({sucess : true , result : result})
}catch{
console.error('Error when getting transactions' , error);
res.status(500).send({ success: false, error: error.message });
}
})

module.exports = router;
}
12 changes: 12 additions & 0 deletions backend/src/routes/transactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require('express');
const {isAuthenticated} = require('../middleware/autMiddleware')
const router = express.Router();
const jwt = require('jsonwebtoken');
const {getUserTransactionsByPage , makeTransaction} = require('../services/transactionService')
const transactionController = require('../controllers/transactionsController')


router.post('/makeTransaction', isAuthenticated, transactionController.transaction);
router.get('/getTransactionByPage/:pageNumber' , isAuthenticated , transactionController.transactions);

module.exports = router;
26 changes: 26 additions & 0 deletions backend/src/routes/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const express = require('express');
const {isAuthenticated} = require('../middleware/autMiddleware')
const router = express.Router();
const jwt = require('jsonwebtoken');
const usersController = require('../controllers/usersController');

// router.post('/checkEmailExists', async (req, res) => {
// const email = req.body.email;
// const emailExists = await checkEmailExists(email);
// res.send({ emailExists });
// });

// router.post('/checkPasswordCorrect', async (req, res) => {
// const email = req.body.email;
// const password = req.body.password;
// const passwordCorrect = await checkPasswordCorrect(email, password);
// res.send({ passwordCorrect });
// });

router.post('/login', usersController.login);
router.post('/signup', usersController.signup);
router.get('/balance', isAuthenticated, usersController.balance);



module.exports = router;
27 changes: 10 additions & 17 deletions backend/src/server.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
//dependancies
const express = require("express");
const session = require("express-session");
const cors = require("cors");
const userRoutes = require("./routes/userRoutes.js");
const createTables = require("./config/createTables.js");
const jwt = require("jsonwebtoken");
//config
const pool = require("./config/db.js");
const createTables = require("./config/createTables.js");
const corsConfig = require("./config/corsConfig");
//middleware
const isAuthenticated = require("./middleware/autMiddleware.js");


//routes
const userRoutes = require("./routes/users.js");
const transactionRoutes = require("./routes/transactions")


const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors({
origin: function (origin, callback) {
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
}));
app.use(corsConfig);

pool.connect((err) => {
if (err) {
Expand All @@ -43,6 +35,7 @@ pool.connect((err) => {
});

app.use('/user', userRoutes);
app.use('/transactions' , transactionRoutes);

const port = 4000

Expand Down
2 changes: 1 addition & 1 deletion backend/src/services/securePassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function hashPassword(password) {

return hashedPassword;
} catch (error) {
throw new Error('Error hashing password');
throw new Error('Error hashing password password was' + password );
}
}

Expand Down
Loading

0 comments on commit 64870df

Please sign in to comment.