Skip to content
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

Amplify.Auth.getCurrentUser() returns AuthUser with incorrect username when using email based auth #3311

Open
2 of 14 tasks
jniemer opened this issue Jun 25, 2023 · 1 comment
Labels
auth Issues related to the Auth Category Documentation Improvements or fixes to public documentation (docs.amplify.aws, pub.dev, readmes). feature-request A request for a new feature or an enhancement to an existing API or category.

Comments

@jniemer
Copy link

jniemer commented Jun 25, 2023

Description

When adding Cognito auth to a project, if email based sign-in is chosen then Amplify.Auth.getCurrentUser() returns an AuthUser that contains the user ID in both userId and username attributes. Expectation would be that username would contain the email address.

Categories

  • Analytics
  • API (REST)
  • API (GraphQL)
  • Auth
  • Authenticator
  • DataStore
  • Notifications (Push)
  • Storage

Steps to Reproduce

  1. Configure Amplify Auth (amplify add auth) to use email based authentication.
  2. Sign-up, confirm and sign-in user
  3. Call Amplify.Auth.getCurrentUser().

Screenshots

No response

Platforms

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Flutter Version

3.10.5

Amplify Flutter Version

1.2.0

Deployment Method

Amplify CLI

Schema

No response

@NikaHsn NikaHsn added auth Issues related to the Auth Category pending-triage This issue is in the backlog of issues to triage labels Jun 26, 2023
@dnys1
Copy link
Contributor

dnys1 commented Jun 27, 2023

Hi @jniemer - this is indeed confusing and I apologize for the poor DX here. The username field represents the username assigned by Cognito. In the case of email/phone number alias, Cognito considers the "username" of the user to be the user ID, which is why that is mirrored in that field.

The reason for this is two-fold:

  • In the case of allowing both phone number and email alias, it is not possible to call one or the other the true username
  • An alias is allowed to change so it is not a permanent value associated with the user

In Amplify Flutter v1, we introduced a new field to the AuthUser which tracks the username value used to sign in. This will exactly match whatever was passed to Amplify.Auth.signIn and, in your case, would be the email of the user.

Here is how to use that API:

final authUser = await Amplify.Auth.getCurrentUser();
final username = switch (authUser.signInDetails) {
  // API-based login means calling the `Amplify.Auth.signIn` API
  CognitoSignInDetailsApiBased(:final username) => username,

  // Otherwise, fallback to the value assigned by Cognito
  _ => authUser.username,
};

@dnys1 dnys1 added Documentation Improvements or fixes to public documentation (docs.amplify.aws, pub.dev, readmes). and removed pending-triage This issue is in the backlog of issues to triage labels Jun 27, 2023
@dnys1 dnys1 self-assigned this Jun 27, 2023
@Jordan-Nelson Jordan-Nelson added the feature-request A request for a new feature or an enhancement to an existing API or category. label Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth Issues related to the Auth Category Documentation Improvements or fixes to public documentation (docs.amplify.aws, pub.dev, readmes). feature-request A request for a new feature or an enhancement to an existing API or category.
Projects
None yet
Development

No branches or pull requests

4 participants