Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions 7-bank-project/api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ app.options('*', cors());
// Configure routes
const router = express.Router();

// Authorization middleware - verify requester is authorized for the target account
const authorizeUser = (req, res, next) => {
const authenticatedUser = req.headers['x-user'];
if (!authenticatedUser || authenticatedUser !== req.params.user) {
return res.status(403).json({ error: 'Unauthorized' });
}
next();
};

// Get server infos
router.get('/', (req, res) => {
return res.send(`${pkg.description} v${pkg.version}`);
Expand Down Expand Up @@ -79,7 +88,7 @@ router.post('/accounts', (req, res) => {
// ----------------------------------------------

// Get all data for the specified account
router.get('/accounts/:user', (req, res) => {
router.get('/accounts/:user', authorizeUser, (req, res) => {
const account = db[req.params.user];

// Check if account exists
Expand All @@ -93,7 +102,7 @@ router.get('/accounts/:user', (req, res) => {
// ----------------------------------------------

// Remove specified account
router.delete('/accounts/:user', (req, res) => {
router.delete('/accounts/:user', authorizeUser, (req, res) => {
const account = db[req.params.user];

// Check if account exists
Expand Down