#How to get Authenticator username in Lambda function?

7 messages · Page 1 of 1 (latest)

pastel topaz
#

How would I get the Authenticator username in a Lambda function? The auth resource is defined under amplify/auth/resource.ts and goes like this:

import { defineAuth } from '@aws-amplify/backend';

/**
 * Define and configure your auth resource
 * @see https://docs.amplify.aws/gen2/build-a-backend/auth
 */
export const auth = defineAuth({
  loginWith: {
    email: true,
  },
  groups: ["lambda"]
});

This is in a React project and uses the <Authenticator> component from @aws-amplify/ui-react (You need to be logged in to even see the button to trigger the Lambda function in the first place).

Tried to do fetchUserAttribtutes in the Lambda function, only to get this error:

{
    "errorType": "AuthUserPoolException",
    "errorMessage": "Auth UserPool not configured.",
    "name": "AuthUserPoolException",
    "recoverySuggestion": "Make sure to call Amplify.configure in your app with userPoolId and userPoolClientId.",
    "stack": [
        "AuthUserPoolException: Auth UserPool not configured.",
        "    at file:///var/task/index.mjs:2:23640",
        "    at y (file:///var/task/index.mjs:2:26389)",
        "    at Mn (file:///var/task/index.mjs:22:4888)",
        "    at pr (file:///var/task/index.mjs:22:5107)",
        "    at file:///var/task/index.mjs:22:5126"
    ]
}

Even though I'm logged in and there's userPoolId in npx ampx sandbox, there is apparenty not a configured UserPool when I try to do fetchUserAttributes. So how would I go about having Lambda return the logged in user?

pulsar crow
#

@pastel topaz, can you share what the frontend code looks like for where you're calling both Amplify.configure() and when you're calling fetchUserAttributes? That error message you're receiving occurs when the fetchUserAttributes() API is called too soon.

Also, are you trying to call this from within the lambda function?

pastel topaz
# pulsar crow <@226416523697192970>, can you share what the frontend code looks like for where...

Hi Chris, I was trying to call it from within the lambda function. But now I'm trying to pass it in as a header. Things have changed, but here is the new code on the frontend (main.tsx):

Amplify.configure(outputs, {
  API: {
    REST: {
      headers: async () => {
        return { Authorization: "authToken" };
      }
    }
  }
}
)
const existingConfig = Amplify.getConfig();
Amplify.configure({
  ...existingConfig,
  API: {
    ...existingConfig.API,
    REST: outputs.custom.API,
  },
});
#

Unfortunately, I have a new error

#

But even stranger is, the permissions should allow it, unless I made some sort of mistake.

#

For the API itself:

const myRestApi = new RestApi(mainStack, "RestApi", {
  restApiName: "myRestApi",
  deploy: true,
  deployOptions: {
    stageName: "dev",
  },
  defaultCorsPreflightOptions: {
    allowOrigins: Cors.ALL_ORIGINS, // Restrict this to domains you trust
    allowMethods: Cors.ALL_METHODS, // Specify only the methods you need to allow
    allowHeaders: [
      ...Cors.DEFAULT_HEADERS,
      'Authorization',
      'Content-Type'
    ], // Specify only the headers you need to allow
  },
});