Skip to content

Commit 44e1cb4

Browse files
author
Trevor Smith
committed
More progress on serverless. Created user and identity pools, some authentication
1 parent f3d7a38 commit 44e1cb4

11 files changed

+46
-10
lines changed

create.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const main = handler(async (event, context) => {
88
TableName: process.env.tableName,
99
Item: {
1010
// The attributes of the item to be created
11-
userId: "123", // The id of the author
11+
userId: event.requestContext.identity.cognitoIdentityId, // The id of the author
1212
noteId: uuid.v1(), // A unique uuid
1313
content: data.content, // Parsed from request body
1414
attachment: data.attachment, // Parsed from request body

delete.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const main = handler(async (event, context) => {
66
TableName: process.env.tableName,
77
// 'Key' defines the partition key and sort key of the item to be removed
88
Key: {
9-
userId: "123", // The id of the author
9+
userId: event.requestContext.identity.cognitoIdentityId, // The id of the author
1010
noteId: event.pathParameters.id, // The id of the note from the path
1111
},
1212
};

get.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const main = handler(async (event, context) => {
66
TableName: process.env.tableName,
77
// 'Key' defines the partition key and sort key of the item to be retrieved
88
Key: {
9-
userId: "123", // The id of the author
9+
userId: event.requestContext.identity.cognitoIdentityId, // The id of the author
1010
noteId: event.pathParameters.id, // The id of the note from the path
1111
},
1212
};

list.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const main = handler(async (event, context) => {
1111
// 'ExpressionAttributeValues' defines the value in the condition
1212
// - ':userId': defines 'userId' to be the id of the author
1313
ExpressionAttributeValues: {
14-
":userId": "123",
14+
":userId": event.requestContext.identity.cognitoIdentityId,
1515
},
1616
};
1717

mocks/create-event.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"body": "{\"content\":\"hello world\",\"attachment\":\"hello.jpg\"}"
2+
"body": "{\"content\":\"hello world\",\"attachment\":\"hello.jpg\"}",
3+
"requestContext": {
4+
"identity": {
5+
"cognitoIdentityId": "USER-SUB-1234"
6+
}
7+
}
38
}

mocks/delete-event.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
22
"pathParameters": {
3-
"id": "578eb840-f70f-11e6-9d1a-1359b3b22944"
3+
"id": "a63c5450-1274-11eb-81db-b9d1e2c85f15"
4+
},
5+
"requestContext": {
6+
"identity": {
7+
"cognitoIdentityId": "USER-SUB-1234"
8+
}
49
}
510
}

mocks/get-event.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
22
"pathParameters": {
3-
"id": "4c5934f0-2f3c-11eb-bf41-c95567f09762"
3+
"id": "cf6a83b0-1314-11eb-9506-9133509a950f"
4+
},
5+
"requestContext": {
6+
"identity": {
7+
"cognitoIdentityId": "USER-SUB-1234"
8+
}
49
}
510
}

mocks/list-event.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
{}
1+
{
2+
"requestContext": {
3+
"identity": {
4+
"cognitoIdentityId": "USER-SUB-1234"
5+
}
6+
}
7+
}

mocks/update-event.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"body": "{\"content\":\"new world\",\"attachment\":\"new.jpg\"}",
33
"pathParameters": {
4-
"id": "578eb840-f70f-11e6-9d1a-1359b3b22944"
4+
"id": "cf6a83b0-1314-11eb-9506-9133509a950f"
5+
},
6+
"requestContext": {
7+
"identity": {
8+
"cognitoIdentityId": "USER-SUB-1234"
9+
}
510
}
611
}

serverless.yml

+10
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ functions:
3838
# Defines an HTTP API endpoint that calls the main function in create.js
3939
# - path: url path is /notes
4040
# - method: POST request
41+
# - authorizer: authenticate using the AWS IAM role
4142
create:
4243
handler: create.main
4344
events:
4445
- http:
4546
path: notes
4647
method: post
48+
authorizer: aws_iam
49+
4750
get:
4851
# Defines an HTTP API endpoint that calls the main function in get.js
4952
# - path: url path is /notes/{id}
@@ -53,6 +56,8 @@ functions:
5356
- http:
5457
path: notes/{id}
5558
method: get
59+
authorizer: aws_iam
60+
5661
list:
5762
# Defines an HTTP API endpoint that calls the main function in list.js
5863
# - path: url path is /notes
@@ -62,6 +67,8 @@ functions:
6267
- http:
6368
path: notes
6469
method: get
70+
authorizer: aws_iam
71+
6572
update:
6673
# Defines an HTTP API endpoint that calls the main function in update.js
6774
# - path: url path is /notes/{id}
@@ -71,6 +78,8 @@ functions:
7178
- http:
7279
path: notes/{id}
7380
method: put
81+
authorizer: aws_iam
82+
7483
delete:
7584
# Defines an HTTP API endpoint that calls the main function in delete.js
7685
# - path: url path is /notes/{id}
@@ -80,3 +89,4 @@ functions:
8089
- http:
8190
path: notes/{id}
8291
method: delete
92+
authorizer: aws_iam

update.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const main = handler(async (event, context) => {
77
TableName: process.env.tableName,
88
// 'Key' defines the partition key and sort key of the item to be updated
99
Key: {
10-
userId: "123", // The id of the author
10+
userId: event.requestContext.identity.cognitoIdentityId, // The id of the author
1111
noteId: event.pathParameters.id, // The id of the note from the path
1212
},
1313
// 'UpdateExpression' defines the attributes to be updated

0 commit comments

Comments
 (0)