Skip to content
Open
Show file tree
Hide file tree
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
21 changes: 19 additions & 2 deletions api/main_endpoints/routes/MembershipPayment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
SERVER_ERROR,
NOT_FOUND,
OK,
UNAUTHORIZED
UNAUTHORIZED,
TOO_MANY_REQUESTS
} = require('../../util/constants').STATUS_CODES;
const membershipState = require('../../util/constants').MEMBERSHIP_STATE;
const { updateMembershipExpiration } = require('../util/userHelpers');
Expand All @@ -21,6 +22,10 @@
const AuditLogActions = require('../util/auditLogActions');
const AuditLog = require('../models/AuditLog');

const attemptCount = new Map();
const MAX_ATTEMPTS = 5;


router.post('/verifyMembership', async (req, res) => {
const decoded = await decodeToken(req, membershipState.PENDING);
if (decoded.status !== OK) {
Expand All @@ -29,6 +34,13 @@

const { confirmationCode } = req.body;
const userId = decoded.token._id;
const attempts = attemptCount.get(userId) ?? 0;

if (attempts >= MAX_ATTEMPTS){
logger.error(`User ${userId} has made too many verification attempts.`); return res.status(TOO_MANY_REQUESTS).json({

Check failure on line 40 in api/main_endpoints/routes/MembershipPayment.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Expected indentation of 4 spaces but found 6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.error(`User ${userId} has made too many verification attempts.`); return res.status(TOO_MANY_REQUESTS).json({
logger.error(`User ${userId} has made too many verification attempts.`);
return res.status(TOO_MANY_REQUESTS).json({

remainingAttempts: 0
});
}

if (!confirmationCode) {
logger.error('Confirmation code missing from verifyMembership request');
Expand All @@ -37,8 +49,11 @@

const paymentDocument = await findVerifyPayment(confirmationCode, userId);
if (!paymentDocument) {
attemptCount.set(userId, attempts + 1);
logger.error('Error verifying payment for user:', userId);
return res.status(NOT_FOUND).send('Error verifying payment.');
return res.status(NOT_FOUND).json({
remainingAttempts: MAX_ATTEMPTS - (attempts + 1)
});
}

const { amount } = paymentDocument;
Expand All @@ -53,6 +68,8 @@
logger.error('Error updating membership expiration for user:', decoded.token._id);
return res.status(SERVER_ERROR).send('Error updating membership expiration.');
}

attemptCount.delete(userId);
logger.info('Membership verified and updated for user:', decoded.token._id);
AuditLog.create({
userId: decoded.token._id,
Expand Down
1 change: 1 addition & 0 deletions api/util/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const STATUS_CODES = {
FORBIDDEN: 403,
NOT_FOUND: 404,
CONFLICT: 409,
TOO_MANY_REQUESTS: 429,
SERVER_ERROR: 500,
};

Expand Down
9 changes: 7 additions & 2 deletions src/APIFunctions/MembershipPayment.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ export async function verifyMembershipFromDb(token, confirmationCode) {
},
body: JSON.stringify({ confirmationCode })
});
status.responseData = res.status;
status.error = !res.ok;
if (res.status == TOO_MANY_ATTEMPTS) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is TOO_MANY_ATTEMPTS defined in this file, if not you can write at the top of the file with the imports:

const TOO_MANY_ATTEMPTS = 429;

for now

const data = await res.json();
status.responseData = data;
status.error = false;
} else {
status.error = !res.ok;
}
} catch (err) {
status.error = true;
status.responseData = err;
Expand Down
4 changes: 4 additions & 0 deletions src/Pages/Profile/MemberView/VerifyMembershipModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export default function VerifyMembershipModal(props) {
user.token,
confirmationCode,
);
if (apiResponse.remainingAttempts != undefined){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (apiResponse.remainingAttempts != undefined){
if (apiResponse.remainingAttempts) {

bannerCallback(`Wrong Code - You have ${apiResponse.remainingAttempts} left.`);
return;
}
Comment on lines 18 to 21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we reach this case we know what kind of error it is, we don't need to give them a status code

if (apiResponse.error) {
bannerCallback(`Unable to verify membership. Please try again later. Status Code: ${apiResponse.responseData || 500}`, 'red');
return;
Expand Down
Loading