Skip to content

Auth.getCurrentUser returns user sub even if user logged in with email #3133

@faltiska

Description

@faltiska

Before opening, please confirm:

Language and Async Model

Java

Amplify Categories

Authentication

Gradle script dependencies

    def aws_amplify_version = '2.30.1'
    coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.1.5")
    implementation "com.amplifyframework:aws-api:$aws_amplify_version"
    implementation "com.amplifyframework:aws-auth-cognito:$aws_amplify_version"
    implementation "com.amplifyframework:core:$aws_amplify_version"

Environment information

------------------------------------------------------------
Gradle 8.14.2
------------------------------------------------------------

Build time:    2025-06-05 13:32:01 UTC
Revision:      30db2a3bdfffa9f8b40e798095675f9dab990a9a

Kotlin:        2.0.21
Groovy:        3.0.24
Ant:           Apache Ant(TM) version 1.10.15 compiled on August 25 2024
Launcher JVM:  17.0.12 (Oracle Corporation 17.0.12+8-LTS-286)
Daemon JVM:    E:\Programare\jdk-17.0.12 (no JDK specified, using current Java home)
OS:            Windows 11 10.0 amd64

Please include any relevant guides or documentation you're referencing

No response

Describe the bug

Auth.getCurrentUser returns user sub even if user logged in with email.

The AWS Mobile Client was returning the email in such cases. The javadoc said:

/**
 * Returns the username attribute of the current access token. Note that the value stored in the username
 * attribute of the access token may vary depending on how sign-in was performed. For example, if the user signed in
 * with email, the username attribute will have the email address.
 * @return The username attribute of the current access token.
 * @see <a href="https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html#amazon-cognito-user-pools-using-the-access-token">Using the Access Token</a>
 * from Cognito documentation.
 */
@AnyThread
public String getUsername() {
    try {
        if (userpoolsLoginKey.equals(mStore.get(PROVIDER_KEY))) {
            return userpool.getCurrentUser().getUserId();
        }
        return null;
    } catch (Exception e) {
        return null;
    }
}

Reproduction steps (if applicable)

No response

Code Snippet

CompletableFuture<String> future = new CompletableFuture<>();
Amplify.Auth.getCurrentUser(
          user -> future.complete(user.getUsername()),
          authException -> future.complete(null)
);

Both the user id and the username attributes in AuthUser are the user sub, regardless of how the user logged in.

Log output

// Put your logs below this line


Configuration File

No response

GraphQL Schema

// Put your schema below this line

Additional information and screenshots

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    authRelated to the Auth category/pluginsfeature-requestRequest a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions