-
Notifications
You must be signed in to change notification settings - Fork 103
Support adding custom attributes #1732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support adding custom attributes #1732
Conversation
🦋 Changeset detectedLatest commit: 400d0bd The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
.changeset/blue-turkeys-trade.md
Outdated
| '@aws-amplify/auth-construct': minor | ||
| --- | ||
|
|
||
| adding customAttributes into userAttributes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We prefer to use imperative verb form.
See https://www.gitkraken.com/learn/git/best-practices/git-commit-message#using-imperative-verb-form
| phoneNumber: DEFAULTS.IS_REQUIRED_ATTRIBUTE.phoneNumber(phoneEnabled), | ||
| ...(props.userAttributes ? props.userAttributes : {}), | ||
| ...(props.userAttributes | ||
| ? Object.entries(props.userAttributes).reduce( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of looping Object.entries(props.userAttributes) twice on line 462 and line 475, we can loop it once and get two arrays.
Something like `{ userAttributes, customAttibutes } = Object.entries(props.userAttributes).reduce(...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this PR is missing changes to auth-construct/API.md, don't forget to run npm run update:api to fix the failing check_api_changes and check_api_extract checks.
| /** | ||
| * Define bindCustomAttribute to meet requirements of the Cognito API to call the bind method | ||
| */ | ||
| public bindCustomAttribute = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like this method should be private. We should test this functionality via constructor inputs. I see there already are some unit tests exercising this; maybe that is sufficient and we don't need the unit tests that are directly calling this method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
packages/auth-construct/src/types.ts
Outdated
| /** | ||
| * CustomAttributes is a union type that represents all the different types of custom attributes. | ||
| */ | ||
| export type CustomAttributes = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| export type CustomAttributes = | |
| export type CustomAttribute = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
| const config: CustomAttributeConfig = { | ||
| dataType: attribute.dataType, | ||
| mutable: attribute.mutable ?? true, | ||
| stringConstraints: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if attribute.dataType === 'Number', do we still want the undefined stringConstraints to be there?
If not, we should conditionally add stringConstraints and numberConstraints
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
| 'custom:display_name': { | ||
| dataType: 'String', | ||
| mutable: true, | ||
| maxLen: 100, | ||
| minLen: 0, | ||
| }, | ||
| 'custom:tenant_id': { | ||
| dataType: 'Number', | ||
| mutable: false, | ||
| max: 66, | ||
| min: 1, | ||
| }, | ||
| 'custom:member_year': { | ||
| dataType: 'Number', | ||
| max: 90, | ||
| min: 0, | ||
| }, | ||
| 'custom:favorite_song': { | ||
| dataType: 'String', | ||
| mutable: true, | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add unit tests for boolean and dateTime as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
| // Conditionally add constraint properties based on dataType. | ||
| if (attribute.dataType === 'String') { | ||
| constraints = { | ||
| ...constraints, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this serve any purpose if constraints was empty to begin with?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
| }; | ||
| } else if (attribute.dataType === 'Number') { | ||
| constraints = { | ||
| ...constraints, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this serve any purpose if constraints was empty to begin with?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
| [key, value] | ||
| ) => { | ||
| if (key.startsWith('custom:')) { | ||
| const attributeKey = key.replace(/^(custom:|User\.?)/i, ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need User. match as well? I don't see corresponding unit tests either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After testing, actually we don't need User. match.
* refactor top level cli error handling * PR updates * Add cause * update api after clean build
Problem
userAttributesdoes not currently support adding custom attributes.Issue number, if available:
Changes
Support adding custom attributes into
userAttributes.Corresponding docs PR, if applicable:
Validation
unit tests
Checklist
run-e2elabel set.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.