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
22 changes: 22 additions & 0 deletions 7-bank-project/api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ router.post('/accounts', (req, res) => {
}

// Create account
const token = crypto.randomBytes(16).toString('hex');
const account = {
user: req.body.user,
currency: req.body.currency,
description: req.body.description || `${req.body.user}'s budget`,
balance: balance || 0,
transactions: [],
token,
};
db[req.body.user] = account;

Expand All @@ -87,6 +89,11 @@ router.get('/accounts/:user', (req, res) => {
return res.status(404).json({ error: 'User does not exist' });
}

// Verify caller identity via token
if (req.headers.authorization !== account.token) {
return res.status(401).json({ error: 'Unauthorized' });
}

return res.json(account);
});

Expand All @@ -101,6 +108,11 @@ router.delete('/accounts/:user', (req, res) => {
return res.status(404).json({ error: 'User does not exist' });
}

// Verify caller identity via token
if (req.headers.authorization !== account.token) {
return res.status(401).json({ error: 'Unauthorized' });
}

// Removed account
delete db[req.params.user];

Expand All @@ -118,6 +130,11 @@ router.post('/accounts/:user/transactions', (req, res) => {
return res.status(404).json({ error: 'User does not exist' });
}

// Verify caller identity via token
if (req.headers.authorization !== account.token) {
return res.status(401).json({ error: 'Unauthorized' });
}

// Check mandatory requests parameters
if (!req.body.date || !req.body.object || !req.body.amount) {
return res.status(400).json({ error: 'Missing parameters' });
Expand Down Expand Up @@ -171,6 +188,11 @@ router.delete('/accounts/:user/transactions/:id', (req, res) => {
return res.status(404).json({ error: 'User does not exist' });
}

// Verify caller identity via token
if (req.headers.authorization !== account.token) {
return res.status(401).json({ error: 'Unauthorized' });
}

const transactionIndex = account.transactions.findIndex(
(transaction) => transaction.id === req.params.id
);
Expand Down