feat: Add Cognito user pool name parameter and update API handlers to include CORS headers
This commit is contained in:
@@ -80,9 +80,9 @@ export const api = createApi({
|
|||||||
baseUrl: process.env.NEXT_PUBLIC_API_BASE_URL,
|
baseUrl: process.env.NEXT_PUBLIC_API_BASE_URL,
|
||||||
prepareHeaders: async (headers) => {
|
prepareHeaders: async (headers) => {
|
||||||
const session = await fetchAuthSession();
|
const session = await fetchAuthSession();
|
||||||
const { accessToken } = session.tokens ?? {};
|
const { idToken } = session.tokens ?? {};
|
||||||
if (accessToken) {
|
if (idToken) {
|
||||||
headers.set("Authorization", `Bearer ${accessToken}`);
|
headers.set("Authorization", `Bearer ${idToken}`);
|
||||||
}
|
}
|
||||||
return headers;
|
return headers;
|
||||||
},
|
},
|
||||||
@@ -120,7 +120,7 @@ export const api = createApi({
|
|||||||
}),
|
}),
|
||||||
invalidatesTags: ["Projects"],
|
invalidatesTags: ["Projects"],
|
||||||
}),
|
}),
|
||||||
getTasks: build.query<Task[], { projectId: number }>({
|
getTasks: build.query<Task[], { projectId: string }>({
|
||||||
query: ({ projectId }) => `tasks?projectId=${projectId}`,
|
query: ({ projectId }) => `tasks?projectId=${projectId}`,
|
||||||
providesTags: (result) =>
|
providesTags: (result) =>
|
||||||
result
|
result
|
||||||
|
|||||||
@@ -40,15 +40,7 @@ provider:
|
|||||||
- "arn:aws:execute-api:${self:provider.region}:*:*/*/POST/users"
|
- "arn:aws:execute-api:${self:provider.region}:*:*/*/POST/users"
|
||||||
|
|
||||||
functions:
|
functions:
|
||||||
postSignUp:
|
# POST /users or triggered by Cognito
|
||||||
handler: src/handlers/postSignUp.handler
|
|
||||||
memorySize: 1024
|
|
||||||
timeout: 60
|
|
||||||
events:
|
|
||||||
- cognitoUserPool:
|
|
||||||
pool: ${ssm:/tasker/cognito/user-pool-id}
|
|
||||||
trigger: PostConfirmation
|
|
||||||
# POST /users
|
|
||||||
createUser:
|
createUser:
|
||||||
handler: src/handlers/createUser.handler
|
handler: src/handlers/createUser.handler
|
||||||
memorySize: 1024
|
memorySize: 1024
|
||||||
@@ -59,6 +51,10 @@ functions:
|
|||||||
method: post
|
method: post
|
||||||
cors: true
|
cors: true
|
||||||
authorizer: aws_iam
|
authorizer: aws_iam
|
||||||
|
- cognitoUserPool:
|
||||||
|
existing: true
|
||||||
|
pool: ${ssm:/tasker/cognito/user-pool-name}
|
||||||
|
trigger: PostConfirmation
|
||||||
# POST /projects
|
# POST /projects
|
||||||
createProject:
|
createProject:
|
||||||
handler: src/handlers/createProject.handler
|
handler: src/handlers/createProject.handler
|
||||||
@@ -151,8 +147,8 @@ functions:
|
|||||||
type: COGNITO_USER_POOLS
|
type: COGNITO_USER_POOLS
|
||||||
arn: ${ssm:/tasker/cognito/user-pool-arn}
|
arn: ${ssm:/tasker/cognito/user-pool-arn}
|
||||||
# GET /tasks/user/${userId}
|
# GET /tasks/user/${userId}
|
||||||
getTasksByUser:
|
getUserTasks:
|
||||||
handler: src/handlers/getTasksByUser.handler
|
handler: src/handlers/getUserTasks.handler
|
||||||
memorySize: 1024
|
memorySize: 1024
|
||||||
timeout: 60
|
timeout: 60
|
||||||
events:
|
events:
|
||||||
|
|||||||
@@ -30,13 +30,19 @@ export const handler = async (event: any): Promise<any> => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: 201,
|
statusCode: 201,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify(newProject),
|
body: JSON.stringify(newProject),
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return {
|
return {
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
message: `Error creating project: ${error.message}`,
|
message: `Error creating project: ${error.message}`,
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -48,13 +48,19 @@ export const handler = async (event: any): Promise<any> => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: 201,
|
statusCode: 201,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify(newTask),
|
body: JSON.stringify(newTask),
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return {
|
return {
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
message: `Error creating task: ${error.message}`,
|
message: `Error creating task: ${error.message}`,
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -10,8 +10,10 @@ const client = new DynamoDBClient({ region: SLS_REGION });
|
|||||||
const docClient = DynamoDBDocument.from(client);
|
const docClient = DynamoDBDocument.from(client);
|
||||||
|
|
||||||
export const handler = async (event: any): Promise<any> => {
|
export const handler = async (event: any): Promise<any> => {
|
||||||
const { username, cognitoId } = JSON.parse(event.body);
|
const username =
|
||||||
const teamId = fetchRandomTeamId();
|
event.request.userAttributes["preferred_username"] || event.userName;
|
||||||
|
const cognitoId = event.userName;
|
||||||
|
const teamId = await fetchRandomTeamId();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const newUser = {
|
const newUser = {
|
||||||
@@ -30,18 +32,10 @@ export const handler = async (event: any): Promise<any> => {
|
|||||||
|
|
||||||
await docClient.put(params);
|
await docClient.put(params);
|
||||||
|
|
||||||
return {
|
console.info(`User ${username} created with teamId ${teamId}`);
|
||||||
statusCode: 201,
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify(newUser),
|
|
||||||
};
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return {
|
throw new Error(`Error creating user: ${error.message}`);
|
||||||
statusCode: 500,
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify({
|
|
||||||
message: `Error creating user: ${error.message}`,
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return event;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -21,13 +21,19 @@ export const handler = async (event: any): Promise<any> => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify(projects.Items),
|
body: JSON.stringify(projects.Items),
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return {
|
return {
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
message: `Error retrieving projects: ${error.message}`,
|
message: `Error retrieving projects: ${error.message}`,
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -54,13 +54,19 @@ export const handler = async (event: any): Promise<any> => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify(tasksWithDetails),
|
body: JSON.stringify(tasksWithDetails),
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return {
|
return {
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
message: `Error retrieving tasks: ${error.message}`,
|
message: `Error retrieving tasks: ${error.message}`,
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -41,13 +41,19 @@ export const handler = async (event: any): Promise<any> => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify(teamsWithUsernames),
|
body: JSON.stringify(teamsWithUsernames),
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return {
|
return {
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
message: `Error retrieving teams: ${error.message}`,
|
message: `Error retrieving teams: ${error.message}`,
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -23,13 +23,19 @@ export const handler = async (event: any): Promise<any> => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify(user.Items?.[0] || {}),
|
body: JSON.stringify(user.Items?.[0] || {}),
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return {
|
return {
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
message: `Error retrieving user: ${error.message}`,
|
message: `Error retrieving user: ${error.message}`,
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -19,13 +19,19 @@ export const handler = async (event: any): Promise<any> => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify(userTasks),
|
body: JSON.stringify(userTasks),
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return {
|
return {
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
message: `Error retrieving tasks for user: ${error.message}`,
|
message: `Error retrieving tasks for user: ${error.message}`,
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -21,13 +21,19 @@ export const handler = async (event: any): Promise<any> => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify(users.Items),
|
body: JSON.stringify(users.Items),
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return {
|
return {
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
message: `Error retrieving users: ${error.message}`,
|
message: `Error retrieving users: ${error.message}`,
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ export const handler = async (event: any): Promise<any> => {
|
|||||||
cognitoId: event.userName,
|
cognitoId: event.userName,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log(postData);
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
hostname: API_BASE_URL ? new URL(API_BASE_URL).hostname : "",
|
hostname: API_BASE_URL ? new URL(API_BASE_URL).hostname : "",
|
||||||
port: 443,
|
port: 443,
|
||||||
@@ -20,6 +22,7 @@ export const handler = async (event: any): Promise<any> => {
|
|||||||
headers: {
|
headers: {
|
||||||
"Content-category": "application/json",
|
"Content-category": "application/json",
|
||||||
"Content-Length": Buffer.byteLength(postData),
|
"Content-Length": Buffer.byteLength(postData),
|
||||||
|
"Allow-Control-Allow-Origin": "*",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -35,5 +38,7 @@ export const handler = async (event: any): Promise<any> => {
|
|||||||
req.end();
|
req.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log(responseBody);
|
||||||
|
|
||||||
return event;
|
return event;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -31,13 +31,19 @@ export const handler = async (event: any): Promise<any> => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify(updatedTask.Attributes),
|
body: JSON.stringify(updatedTask.Attributes),
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return {
|
return {
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
message: `Error updating task: ${error.message}`,
|
message: `Error updating task: ${error.message}`,
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -83,6 +83,13 @@ resource "aws_ssm_parameter" "user_pool_arn" {
|
|||||||
value = aws_cognito_user_pool.tasker_cognito_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" {
|
resource "aws_ssm_parameter" "client_id" {
|
||||||
name = "/tasker/cognito/client-id"
|
name = "/tasker/cognito/client-id"
|
||||||
description = "Tasker Cognito Client ID"
|
description = "Tasker Cognito Client ID"
|
||||||
|
|||||||
Reference in New Issue
Block a user