-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
58 lines (52 loc) · 1.47 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
const { DynamoDBDocumentClient, UpdateCommand, GetCommand, ScanCommand } = require("@aws-sdk/lib-dynamodb");
const client = new DynamoDBClient({});
const dynamo = DynamoDBDocumentClient.from(client);
const tableName = process.env.TABLE_NAME;
const maxRequestsPerMonth = process.env.MONTHLY_LIMIT;
exports.handler = async (event) => {
console.time();
try {
const {Item} = await dynamo.send(
new GetCommand({
TableName: tableName,
Key: {
id: 1
}
})
);
console.timeEnd();
const currentMonth = new Date().getUTCMonth();
// If user has exceeded 10 requests this month, deny access
if(Item?.count >= maxRequestsPerMonth && Item?.month === currentMonth) {
console.timeEnd();
console.error("Exceeded max requests");
return { isAuthorized: false };
}
// If month has changed, reset count to 1
const newCount = Item?.month === currentMonth ? Item.count + 1 : 1;
await dynamo.send(
new UpdateCommand({
TableName: tableName,
Key: {
id: 1
},
UpdateExpression: `SET #month = :currentMonth, #count = :newCount`,
ExpressionAttributeValues: {
":currentMonth": currentMonth,
":newCount": newCount
},
ExpressionAttributeNames: {
"#count": "count",
"#month": "month"
},
})
);
console.timeEnd();
return { isAuthorized: true };
} catch (error) {
console.timeEnd();
console.error(error);
return { isAuthorized: false };
}
};