aws-amplify / amplify-backend

Home to all tools related to Amplify's code-first DX (Gen 2) for building fullstack apps on AWS
Apache License 2.0
158 stars 54 forks source link

Error Stating StandardAttribute Doesn't Exist When it Does #1703

Closed mitchelllee222 closed 2 months ago

mitchelllee222 commented 2 months ago

Environment information

System:
  OS: Windows 11 10.0.22631
  CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
  Memory: 41.60 GB / 63.88 GB
Binaries:
  Node: 20.10.0 - C:\Program Files\nodejs\node.EXE
  Yarn: 3.5.0 - C:\Program Files\nodejs\yarn.CMD
  npm: 10.2.5 - C:\Program Files\nodejs\npm.CMD
  pnpm: 9.4.0 - C:\Program Files\nodejs\pnpm.CMD
NPM Packages:
  @aws-amplify/backend: 1.0.0
  @aws-amplify/backend-cli: 1.0.1
  aws-amplify: 6.3.8
  aws-cdk: 2.140.0
  aws-cdk-lib: 2.140.0
  typescript: 5.4.5
AWS environment variables:
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
  AWS_STS_REGIONAL_ENDPOINTS = regional
No CDK environment variables

Description

I'm in the process of converting my gen 1 app to a gen 2 app. I'm attempting to setup authentication. My app previously used Cognito with email to log in, but also required user to provide name and phone_number attributes. I configured my resource.ts as such:

`import { defineAuth } from "@aws-amplify/backend"

export const auth = defineAuth({ loginWith: { email: true, }, userAttributes: { name: { mutable: true, required: true, }, phone_number: { mutable: true, required: true, }, }, }) ` According to the gen 2 documentation, userAttributes can be used to define any standard Cognito attributes, which name and phone_number are both standard attributes. However, when it goes to compile, I get this error:

2024-07-02T04:12:28.597Z [INFO]: amplify/auth/resource.ts(8,5): error TS2353: Object literal may only specify known properties, and 'name' does not exist in type 'StandardAttributes'.

Followed documentation from: https://docs.amplify.aws/react/build-a-backend/auth/concepts/user-attributes/ Which links to list of standard attributes: https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html#cognito-user-pools-standard-attributes

josefaidt commented 2 months ago

Hey @mitchelllee222 :wave: thanks for raising this! While we work to improve our API documentation the attributes listed in this object are camelCase and align with CDK's StandardAttributes https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cognito.StandardAttributes.html#properties

mitchelllee222 commented 2 months ago

Thank you! I updated the attributes to use fullname and phoneNumber as listed in the doc you listed and was able to get past the previous issue. However, this introduced a new issue in which now it says that I can't update the userpool that was already created: 90

2024-07-02T19:05:20.571Z [INFO]: CFNUpdateNotSupportedError: User pool attributes cannot be changed after a user pool has been created.

91

Resolution: To change these attributes, remove defineAuth from your backend, deploy, then add it back. Note that removing defineAuth and deploying will delete any users stored in your UserPool.

I apologize, but I am not following what the Resolution is telling me to do to resolve this. I had no control over the initial creation of the UserPool and I'm okay with the warning about it deleting any users since I'm just setting up the app, but I'm not sure how I update the resource.ts file appropriately to make the changes.

josefaidt commented 2 months ago

Hey @mitchelllee222 this is a Cognito limitation where required attributes cannot be changed after initial creation https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html#how-to-edit-standard-attributes

To delete it you can comment out/remove the auth resource from defineBackend, push to git to allow it to deploy, then add it back

import { defineBackend } from "@aws-amplify/backend"
import { auth } from "./auth/resource"
import { data } from "./data/resource"

defineBackend({
- auth,
  data,
})
mitchelllee222 commented 2 months ago

Thank you Jose, now that I have some clearer understanding how gen 2 works, I just deleted the app, updated the resource.ts in Auth to correctly have the attributes I want and created the app again. Now I'm off to the races with the correct properties defined in project using the property names from the doc you shared.