Hugo Zapata
Hugo Zapata

Reputation: 4474

How to get the current user from a Lambda resolver in AWS Amplify (GraphQL api and NodeJS Lambda)?

I have a graphql Api and added a lambda function to resolve a mutation.

type Mutation {
  addTeamMember(email: String!, teamId:ID!): String @function(name: "add-team-member-${env}")
}

From the lambda I want to retrieve the authenticated user that sent the request to perform additional validations, ¿How to retrieve it from the request information?

strong text

Upvotes: 2

Views: 703

Answers (2)

Zac
Zac

Reputation: 730

To access event.identity in lambda resolver, we need to configure the request mapping template as below.

{
  "version": "2017-02-28",
  "operation": "Invoke",
  "payload": {
        "args": $utils.toJson($context.args),
        "identity":  $utils.toJson($context.identity)
    }
}

Note: The identity field isn't populated if you use API_KEY authorization.

References: https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html

https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html#aws-appsync-resolver-context-reference-identity

Upvotes: 0

Hugo Zapata
Hugo Zapata

Reputation: 4474

I printed the contents of the first lambda parameter and found it:

Access it with:

event.identity.username

Upvotes: 4

Related Questions