【问题标题】:AWS cognito pre auth lambda triggerAWS cognito pre auth lambda 触发器
【发布时间】:2023-01-01 23:39:52
【问题描述】:

我目前正在从事一个用户管理项目,我正在尝试在 AWS cognito 中实施预身份验证 lambda 触发器,该触发器在外部数据库中检查经过身份验证的用户拥有的订阅并将其返回。 有人可以帮我举个例子吗?这是我第一次使用 aws cognito

我尝试添加查询以检查外部数据库中的用户订阅,但没有成功。

【问题讨论】:

    标签: aws-lambda amazon-cognito eventtrigger


    【解决方案1】:

    要在 AWS Cognito 中使用预身份验证 Lambda 触发器,您需要创建一个 Lambda 函数并将其附加到 PreAuthentication 属性。

    CloudFormation 模板示例

    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
    
    

    示例 Lambda 函数

    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;
    };
    

    见:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpool.html#cfn-cognito-userpool-lambdaconfig

    【讨论】:

      猜你喜欢
      • 2019-06-12
      • 2018-03-11
      • 2020-08-15
      • 2018-03-10
      • 2018-03-04
      • 2018-04-02
      • 2019-05-15
      • 2016-09-22
      • 2022-07-21
      相关资源
      最近更新 更多