【发布时间】:2023-01-01 23:39:52
【问题描述】:
我目前正在从事一个用户管理项目,我正在尝试在 AWS cognito 中实施预身份验证 lambda 触发器,该触发器在外部数据库中检查经过身份验证的用户拥有的订阅并将其返回。 有人可以帮我举个例子吗?这是我第一次使用 aws cognito
我尝试添加查询以检查外部数据库中的用户订阅,但没有成功。
【问题讨论】:
标签: aws-lambda amazon-cognito eventtrigger
我目前正在从事一个用户管理项目,我正在尝试在 AWS cognito 中实施预身份验证 lambda 触发器,该触发器在外部数据库中检查经过身份验证的用户拥有的订阅并将其返回。 有人可以帮我举个例子吗?这是我第一次使用 aws cognito
我尝试添加查询以检查外部数据库中的用户订阅,但没有成功。
【问题讨论】:
标签: aws-lambda amazon-cognito eventtrigger
要在 AWS Cognito 中使用预身份验证 Lambda 触发器,您需要创建一个 Lambda 函数并将其附加到 PreAuthentication 属性。
Resources:
ExampleUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: ExampleUserPool
LambdaConfig:
PreAuthentication:
- LambdaTrigger: true
LambdaFunctionArn: !GetAtt PreAuthLambdaFunction.Arn
PreAuthLambdaFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: PreAuthLambdaFunction
Runtime: nodejs18.x
exports.handler = async (event) => {
// Extract attributes from the event object
const { username } = event.request.userAttributes;
// Check an external data base the subscription the authenticated user has
const subscriptionStatus = await checkExternalDatabase(username);
// Stop Cognito flow based on subscription status
if (subscriptionStatus !== 'ACTIVE') {
throw new Error('User does not have a valid subscription');
}
// Continue Cognito flow
return event;
};
【讨论】: