使用Cognito用户池将userId从angular传递到AWS Lambda

问题描述

我有一个有角度的前端运送表单,当我单击“提交”时,值到达后端,但没有到达userId。我的应用通过Cognito用户池进行身份验证,并且我获得了令牌。

enter image description here

但我似乎无法传递userId。

enter image description here

我为documentation添加$context.authorizer.principalId。我也尝试过$context.authorizer.claims.userId,但问题相同。

enter image description here

shipping.service.ts

import { HttpClient,HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Http,Headers,Response } from '@angular/http';
import { Subject,BehaviorSubject } from 'rxjs';
import { map } from 'rxjs/operators';
import { AuthService } from '../../auth.service';

@Injectable()
export class ShippingService {
  dataEdited = new BehaviorSubject<boolean>(false);
  dataIsLoading = new BehaviorSubject<boolean>(false);
  dataLoaded = new Subject<[]>();
  dataLoadFailed = new Subject<boolean>();
  userData;
  constructor(private http: HttpClient,private authService: AuthService) {
  }

  onStoreData(data) {
    this.dataLoadFailed.next(false);
    this.dataIsLoading.next(true);
    this.dataEdited.next(false);
    this.userData = data;
    console.log("DATA",data)


    this.authService.getAuthenticatedUser().getSession((err,session) => {
      console.log(session);
      let headers = new HttpHeaders();
      headers.append('Authorization',session.getIdToken().getJwtToken());
      headers.append('Accept','application/json');
      headers.append('Access-Control-Allow-Origin','*');
      let options = { headers: headers,reportProgress: true };

      if (err) {
        return;
      }
      this.http.post('https://nx96w6r3oc.execute-api.us-east-1.amazonaws.com/dev/shipping',data,options)

        .subscribe(
          (result) => {
            this.dataLoadFailed.next(false);
            this.dataIsLoading.next(false);
            this.dataEdited.next(true);
          },(error) => {
            this.dataIsLoading.next(false);
            this.dataLoadFailed.next(true);
            this.dataEdited.next(false);
          }
        );
    });
  }
}

aws lambda函数

const AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

exports.handler =  (event,context,callback) => {
    console.log(event)
    const params = {
    Item:{
        "UserId": {
            S: event.userId
        },"FirstName": {
            S: event.firstName
        },"LastName": {
            S: event.lastName
        },"Address1": {
            S: event.address1
        },"Address2": {
            S: event.address1
        },"State": {
            S: event.state
        },"City": {
            S: event.city
        },"Zip": {
            S: event.zip
        }
        
    },TableName: "shipping"
    };
    
 dynamodb.putItem(params,function(err,data) {
    
   if (err) {
       console.log(err)
    callback(err);
   } 
   else{ 
           console.log(data)
callback(null,data);
   }
 });


   
 };

更新:

到目前为止,我注意到的几件事是在lambda函数输出console.log(context)时身份未定义。

enter image description here

我还在上面的角度服务中做了一个console.log(session)来验证user_id是否存在

enter image description here

我非常困惑为什么userId在输出显示为空白字符串userId: ''。感谢您的帮助!

解决方法

您可以通过以下方式获得想要的东西:

context.identity.cognitoIdentityId
,

有两件事是问题所在。我通过以下方式解决了问题

  1. 为我的用户池userId更改集成请求映射模板userId值"userId": "$context.authorizer.claims.sub"
  2. 我要完全解决的另一件事是在方法请求中添加我创建的授权者。

进行这些更改后,我能够成功地将数据存储到dynamodb中。

enter image description here