feat: Add Cognito user pool name parameter and update API handlers to include CORS headers

This commit is contained in:
2024-11-23 11:12:46 +02:00
parent 59c0c97252
commit 5f9ee29c8d
14 changed files with 103 additions and 47 deletions

View File

@@ -40,15 +40,7 @@ provider:
- "arn:aws:execute-api:${self:provider.region}:*:*/*/POST/users"
functions:
postSignUp:
handler: src/handlers/postSignUp.handler
memorySize: 1024
timeout: 60
events:
- cognitoUserPool:
pool: ${ssm:/tasker/cognito/user-pool-id}
trigger: PostConfirmation
# POST /users
# POST /users or triggered by Cognito
createUser:
handler: src/handlers/createUser.handler
memorySize: 1024
@@ -59,6 +51,10 @@ functions:
method: post
cors: true
authorizer: aws_iam
- cognitoUserPool:
existing: true
pool: ${ssm:/tasker/cognito/user-pool-name}
trigger: PostConfirmation
# POST /projects
createProject:
handler: src/handlers/createProject.handler
@@ -151,8 +147,8 @@ functions:
type: COGNITO_USER_POOLS
arn: ${ssm:/tasker/cognito/user-pool-arn}
# GET /tasks/user/${userId}
getTasksByUser:
handler: src/handlers/getTasksByUser.handler
getUserTasks:
handler: src/handlers/getUserTasks.handler
memorySize: 1024
timeout: 60
events:

View File

@@ -30,13 +30,19 @@ export const handler = async (event: any): Promise<any> => {
return {
statusCode: 201,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(newProject),
};
} catch (error: any) {
return {
statusCode: 500,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error creating project: ${error.message}`,
}),

View File

@@ -48,13 +48,19 @@ export const handler = async (event: any): Promise<any> => {
return {
statusCode: 201,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(newTask),
};
} catch (error: any) {
return {
statusCode: 500,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error creating task: ${error.message}`,
}),

View File

@@ -10,8 +10,10 @@ const client = new DynamoDBClient({ region: SLS_REGION });
const docClient = DynamoDBDocument.from(client);
export const handler = async (event: any): Promise<any> => {
const { username, cognitoId } = JSON.parse(event.body);
const teamId = fetchRandomTeamId();
const username =
event.request.userAttributes["preferred_username"] || event.userName;
const cognitoId = event.userName;
const teamId = await fetchRandomTeamId();
try {
const newUser = {
@@ -30,18 +32,10 @@ export const handler = async (event: any): Promise<any> => {
await docClient.put(params);
return {
statusCode: 201,
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newUser),
};
console.info(`User ${username} created with teamId ${teamId}`);
} catch (error: any) {
return {
statusCode: 500,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
message: `Error creating user: ${error.message}`,
}),
};
throw new Error(`Error creating user: ${error.message}`);
}
return event;
};

View File

@@ -21,13 +21,19 @@ export const handler = async (event: any): Promise<any> => {
return {
statusCode: 200,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(projects.Items),
};
} catch (error: any) {
return {
statusCode: 500,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving projects: ${error.message}`,
}),

View File

@@ -54,13 +54,19 @@ export const handler = async (event: any): Promise<any> => {
return {
statusCode: 200,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(tasksWithDetails),
};
} catch (error: any) {
return {
statusCode: 500,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving tasks: ${error.message}`,
}),

View File

@@ -41,13 +41,19 @@ export const handler = async (event: any): Promise<any> => {
return {
statusCode: 200,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(teamsWithUsernames),
};
} catch (error: any) {
return {
statusCode: 500,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving teams: ${error.message}`,
}),

View File

@@ -23,13 +23,19 @@ export const handler = async (event: any): Promise<any> => {
return {
statusCode: 200,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(user.Items?.[0] || {}),
};
} catch (error: any) {
return {
statusCode: 500,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving user: ${error.message}`,
}),

View File

@@ -19,13 +19,19 @@ export const handler = async (event: any): Promise<any> => {
return {
statusCode: 200,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(userTasks),
};
} catch (error: any) {
return {
statusCode: 500,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving tasks for user: ${error.message}`,
}),

View File

@@ -21,13 +21,19 @@ export const handler = async (event: any): Promise<any> => {
return {
statusCode: 200,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(users.Items),
};
} catch (error: any) {
return {
statusCode: 500,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving users: ${error.message}`,
}),

View File

@@ -10,6 +10,8 @@ export const handler = async (event: any): Promise<any> => {
cognitoId: event.userName,
});
console.log(postData);
const options = {
hostname: API_BASE_URL ? new URL(API_BASE_URL).hostname : "",
port: 443,
@@ -20,6 +22,7 @@ export const handler = async (event: any): Promise<any> => {
headers: {
"Content-category": "application/json",
"Content-Length": Buffer.byteLength(postData),
"Allow-Control-Allow-Origin": "*",
},
};
@@ -35,5 +38,7 @@ export const handler = async (event: any): Promise<any> => {
req.end();
});
console.log(responseBody);
return event;
};

View File

@@ -31,13 +31,19 @@ export const handler = async (event: any): Promise<any> => {
return {
statusCode: 200,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(updatedTask.Attributes),
};
} catch (error: any) {
return {
statusCode: 500,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error updating task: ${error.message}`,
}),

View File

@@ -83,6 +83,13 @@ resource "aws_ssm_parameter" "user_pool_arn" {
value = aws_cognito_user_pool.tasker_cognito_user_pool.arn
}
resource "aws_ssm_parameter" "user_pool_name" {
name = "/tasker/cognito/user-pool-name"
description = "Tasker Cognito User Pool Name"
type = "String"
value = aws_cognito_user_pool.tasker_cognito_user_pool.name
}
resource "aws_ssm_parameter" "client_id" {
name = "/tasker/cognito/client-id"
description = "Tasker Cognito Client ID"