question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow to set read and write attributes in Cognito UserPoolClient

See original GitHub issue

When a client of a Cognito User Pool is created, the developer can (and should) specify which user attributes the client will be able to read and/or write.

This is not currently implemented in Cognito.

Use Case

Often you want different clients to be able to make different operations on user attributes. For example, there might be a customerPlan attribute for every user but only some clients (i.e. those used by the back office) are allowed to change its value.

Or you may want to allow a specific client (e.g. the one used by the web app) to be able to read the email attribute but not the `

Proposed Solution

The proposed solution is to allow to set the list of read attributes and write attributes to the props for userPoolClient.

As example

const pool = new UserPool(stack, 'Pool');
pool.addClient('MyClient', {
  readAttributes: ['email', 'phoneNumber', 'custom:customerPlan', 'custom:isActive']
  writeAttributes: ['phoneNumber', 'custom:isActive']
});

I think there are two flaws with this approach:

  1. having to type the attribute names is error prone
  2. having to prepend custom: to all custom attributes can be easily forgot

The solution for both problems could be the following:

const pool = new UserPool(stack, 'Pool');
pool.addClient('MyClient', {
  readAttributes: {
    // all cognito attributes are automatically available as boolean values
    email: true,
    phoneNumber: true,
    // all custom attributes can be set with an array without the need to prepend them with `custom:`
    custom: ['customerPlan', 'isActive'],
  },
  writeAttributes: {
    // all cognito attributes are available as boolean values
    email: false,
    phoneNumber: true,
    // all custom attributes can be set with an array without the need to prepend them with `custom:`
    custom: ['isActive'],
  },
});

Other

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
stevensnoeijencommented, Oct 13, 2020

I’m using this workaround for now:

        const client = userPool.addClient('client', {
            userPoolClientName: 'client',
            generateSecret: false,
            disableOAuth: true,
        });
        const cfnClient = client.node.defaultChild as CfnUserPoolClient;
        cfnClient.readAttributes = [
            // customs
            'custom:validated_email',
        ];
        cfnClient.writeAttributes = [
            // defaults
            'email',
            'family_name',
            'gender',
            'given_name',
            // customs
            'custom:validated_email',
        ];
1reaction
jonathanbiardcommented, Oct 31, 2020

We started a project using Amplify and we are now replacing it with the CDK. As it stands right now, I cannot set the read/write attributes in the same way as Amplify was doing it because there is no option in pool.addClient() / UserPoolClientOptions for this.

I will try @stevensnoeijen 's workaround for now but it would be nice not to have to use a workaround.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AWS::Cognito::UserPoolClient - AWS CloudFormation
The user pool attributes that the app client can write to. If your app client allows users to sign in through an IdP,...
Read more >
Read/Write to Cognito DeveloperOnlyAttribute - Stack Overflow
A user can login with their username/password, and then make a call to updateUserAttributes() to change any DeveloperOnlyAttributes defined ...
Read more >
update-user-pool-client — AWS CLI 2.8.7 Command Reference
Updates the specified user pool app client with the specified attributes. You can get a list of the current user pool app client...
Read more >
Configuring a user pool app client - Amazon Cognito
After you create a user pool, you can configure an app client to use the built-in webpages ... (Optional) Choose Set attribute read...
Read more >
serverless-cognito-add-custom-attributes-alifensome - npm
This plugin allows you to add custom attributes to an existing ... a User Pool Client, giving that client read and write permissions...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found