aws-amplify / amplify-category-api

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development. This plugin provides functionality for the API category, allowing for the creation and management of GraphQL and REST based backends for your amplify project.
https://docs.amplify.aws/
Apache License 2.0
89 stars 79 forks source link

Issue with create mutation concatenating the username twice #3000

Closed rafaelfaria closed 1 week ago

rafaelfaria commented 3 weeks ago

How did you install the Amplify CLI?

npm

If applicable, what version of Node.js are you using?

No response

Amplify CLI Version

12.13.0

What operating system are you using?

Mac

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No manual changes made

Describe the bug

When creating a new record using Cognito authentication in an Amplify project, the username field is being populated incorrectly. The username value appears duplicated in the format userID::userID instead of the expected single userID format. This is affecting our ability to query and manage records accurately, as the concatenated format does not match our expected schema.

It creates the record, but the response comes null for all the stuff that needs to relate to.

Expected behavior

The username field should only contain the user’s Cognito ID as a single value (e.g., userID), without any additional concatenation.

Reproduction steps

  1. Define a model in the schema with a field (e.g., username) marked with @auth rules that depend on owner and allow: owner with custom ownerField.

    type League @model
    @auth(
    rules: [
      { allow: owner, ownerField: "username" },
      { allow: private, provider: iam }
    ]
    )
    {
    id: ID!
    clubID: ID! @index(name: "byClubLeague", queryField: "listLeaguesByClubId")
    club: Club @belongsTo(fields: ["clubID"])
    name: String!
    tournaments: [Tournament] @hasMany(indexName: "byLeagueTournament", fields: ["id"])
    username: String
    }
  2. Use Cognito to create a new record for this model, ensuring that Cognito automatically populates the username.

    async create(input: CreateLeagueInput) {
    
    const { data } = await API.graphql(
      graphqlOperation(createLeague, { input: { ...input } })
    ) as CreateLeagueResult;
    
    if (!data) {
      throw new Error('There was an issue trying to create a new league');
    }
    
    return data.createLeague;
    }
  3. Query or inspect the new record, observing that the username field contains a duplicated userID::userID instead of the expected single user ID. You will get something like "244884a8-20c1-7099-7bab-b1e8c8eef5fb::244884a8-20c1-7099-7bab-b1e8c8eef5fb"

image

Project Identifier

253d375723e7e6353e94c2aed253815a

Log output

``` # Put your logs below this line ```

Additional information

The issue is occurring in an environment where we use Amplify's @auth directive with owner authorization. This unintended concatenation might be due to the IAM and Cognito configurations or a possible bug in the auto-population of owner fields by Amplify. Creating a lambda function to create the league through IAM in the backend works but is not a sustainable solution.

Besides, due to these issues, I am getting a lot of errors like below, which I am assuming is due of this issue with the double format username.

image

Before submitting, please confirm:

sundersc commented 3 weeks ago

@rafaelfaria This is the default behavior. When amplify auto populates the owner field, it uses sub::username format. Depending on the cognito configuration, sub and username may be the same. If you explicitly pass the username in the request, then it should be respected. But currently there is no option to configure how the auto-populate works.

rafaelfaria commented 3 weeks ago

Interesting. And even thou the permissions are set like that, it still respect the auth? meaning, it is either sub::username or just username?

sundersc commented 3 weeks ago

Yes, it will still respect the auth. Recommendation is to use the default sub::username. However we support just the sub or username as well.

github-actions[bot] commented 1 week ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.