amplify-js: Remaining issues when merging federated users with native Cognito accounts in PreSignUp Trigger

How did you install the Amplify CLI?

npm

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

18

Amplify CLI Version

12.1.1

What operating system are you using?

WSL2 on Windows

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

no

Describe the bug

I am using a setup very close to what the OP documented in this issue (although not in a Flutter, but an Angular application): https://github.com/aws-amplify/amplify-flutter/issues/1716

Thanks to the work being done on that issue, we do not have to throw a custom exception and reinitiate the auth flow on the client-side any longer on successfully linking a social provider!

There are still a couple of issues remaining, though, leading to an overall painful developer experience when implementing account merging, which I am documenting here. All of them are focused around Cognito.

Anyone who ran into these issues, as well, please upvote to raise visibility.

List of Issues

1. Missing ability to map (or at least hardcode to true) the “email_verified” user attribute for Facebook (and apparently some other non-Google) IDPs

For Google, we can map the email_verified attribute to Cognito user attributes. For Facebook and some other IDPs, this is not possible, atm.

By default, mapped email addresses are unverified. You can’t verify a mapped email address using a one-time code. Instead, map an attribute from your IdP to get the verification status. For example, Google and most OIDC providers include the email_verified attribute.

When a user signs in through an IdP, Amazon Cognito updates the mapped attributes with the latest information from the IdP. Amazon Cognito updates each mapped attribute, even if its current value already matches the latest information.

Source

Issue: Setting “email_verified” manually on signup won’t be enough. If there is no attribute mapping present, Cognito sets the attribute to false on each login. Users with an unverified email won’t be able to use the forgot password flow, even though they have a native account.

Current workaround: Call adminUpdateUserAttributes to set “email_verified” to true in the PreToken generation trigger as described here.

Why does it have to be the PreTokenGeneration trigger? Because it is the only one being called on the first and all subsequent social signings (see this doc)

Obviously, setting this attribute on each login is a sh**** workaround. And it is not straightforward to implement because of the next issue.

==================================================================================

2. Cyclic dependency for PreTokenGeneration lambda

The PreTokenGeneration lambda now needs permissions on auth to call AdminUpdateUserAttributes. When trying to add it, though, there is a cyclic dependency error, similar to what’s documented here.

We’ve dealt with cyclic dependency issues in Cognito triggers a lot in the past, and I’m surprised that most issues around this topic are closed, even though issues persists. My workaround, which I also got from one of these older Github issues, is to rely on the UserPoolId to be exported from somewhere else by adding this to parameters.json (cognitoUserPoolId is the value from cli-inputs.json):

  "<cognitoUserPoolId>UserPoolId": {
    "Fn::GetAtt": ["<cognitoUserPoolId>", "Outputs.UserPoolId"]
  }

Along with this in custom-policies.json

[
  {
    "Effect": "Allow",
    "Action": ["cognito-idp:AdminUpdateUserAttributes"],
    "Resource": [
      {
        "Fn::Join": [
          "",
          [
            "arn:aws:cognito-idp:",
            {
              "Ref": "AWS::Region"
            },
            ":",
            {
              "Ref": "AWS::AccountId"
            },
            ":userpool/",
            {
              "Ref": "<cognitoUserPoolId>UserPoolId"
            }
          ]
        ]
      }
    ]
  }
]

==================================================================================

3. Calling AdminSetUserPassword confirms the user, but does not trigger the PostConfirmation hook

We create user resources in the PostConfirmation hook and need it called for every user, not just the ones creating a native Cognito account. We considered calling AdminConfirmSignUp as a workaround, which brings me to the next issue.

==================================================================================

4. AdminConfirmSignUp throws “user already confirmed” error

AdminConfirmSignUp according to Cognito docs would trigger the PostConfirmation hook when running successfully, but it throws a “user already confirmed” error when called after AdminSetUserPassword.

When calling AdminConfirmSignUp before AdminSetUserPassword it throws an error with an internal stacktrace saying it’s unable to JSON.stringify a cyclic object structure. (Sorry, forgot to copy the stacktrace here.)

According to a Stackoverflow post, AdminConfirmSignUp wouldn’t even trigger the PostConfirmation hook, which, if true, contradicts the docs. I cannot confirm whether this is true, because at this point, I decided to revert to calling the PostConfirmation hook manually.

==================================================================================

5. AdminLinkProviderForUser randomly throws an InvalidParameterException

AdminLinkProviderForUser randomly throws this InvalidParameterException:

"Invalid SourceUser: Cognito users with a username/password may not be passed in as a SourceUser, only as a DestinationUser"

I double-checked a million times whether I had mixed up source and destination user, but I hadn’t. This came up for others, as there is a reference to it in this issue.

Apparently, some people were able to mitigate it by assigning more memory to the lambda function. This does not work reliably in my case. My current workaround is to catch and ignore InvalidParameterExceptions for the AdminLinkProviderForUser call. From what I see in the Cognito console, it still seems to successfully link social providers with native users. I don’t know if there are any side effects I’m unaware of.

Expected behavior

Better developer experience and tested documentation on how to implement merging social provider with native accounts.

Reproduction steps

Try implementing account merging as described here by the OP: https://github.com/aws-amplify/amplify-flutter/issues/1716

Project Identifier

809daeeb99aae0ff84c097e535188112

Log output

# Put your logs below this line


Additional information

No response

Before submitting, please confirm:

  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • I have removed any sensitive information from my code snippets and submission.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 6
  • Comments: 16 (6 by maintainers)

Most upvoted comments

Hi I am having same problem number 5, I checked destination is correct, it could be in fact a racing condition but I am not sure how to avoid it as all seems right at the moment

I am also having the same issue with number 5.

Okay, let me give a set of instructions, as best as I can:

  1. Set up a new amplify app with Angular.(*)
  2. Choose Cognito userpool for authentication
  3. Add Facebook as a social provider.
  4. Use the Authenticator from the amplify angular-ui npm package.
  5. Merge the Facebook federated identity with a native Cognito user in the PreSignup lambda. Use the code from the linked issue for this (meaning we create a native Cognito user if someone signs up with Facebook for the first time and link it with the Facebook identity. If a native account already existed, because the user created it before, we also link it.)
  6. Try to get the forgot password flow for the native user working even after they signed in with Facebook, which set their email to unverified in the user pool.
  7. Realize that you’ll also run into the other issues mentioned above.

Don’t hesitate to ask for clarification, if any of these steps are unclear or need more details.

(*) The issues are not specific to Angular, it would probably work the same to set up an example with React or Vue, as long as there is a amplify-ui-package with an authenticator component that supports Facebook login and the forgot password flow.