The way the tutorial uses the authorization is like this since they’re using DynamoDB:
export const main = handler(async (event, context) => {
const data = JSON.parse(event.body);
const params = {
TableName: process.env.tableName,
// 'Item' contains the attributes of the item to be created
// - 'userId': user identities are federated through the
// Cognito Identity Pool, we will use the identity id
// as the user id of the authenticated user
// - 'noteId': a unique uuid
// - 'content': parsed from request body
// - 'attachment': parsed from request body
// - 'createdAt': current Unix timestamp
Item: {
userId: event.requestContext.identity.cognitoIdentityId,
noteId: uuid.v1(),
content: data.content,
attachment: data.attachment,
createdAt: Date.now()
}
};
await dynamoDb.put(params);
return params.Item;
});
So the parameters are passed to the dynamoDb instance and it take cares of the authorization but how can I do the same using a postgres database? Currently my code looks like this:
export const getOne = handler(async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false;
try {
const { User } = await connectToDatabase();
const user = await User.findOne({
where: { enterprise_email: event.queryStringParameters.email },
});
if (!user)
throw new Error(`User with id: ${event.queryStringParameters.email} was not found`);
return user;
} catch (err) {
throw ('Could not fetch the user.', err);
}
});