aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.43k stars 2.13k forks source link

userPool.getCurrentUser().username returns alias instead of username for custom auth #7139

Open SNagappan opened 4 years ago

SNagappan commented 4 years ago

I experienced the exact same bug on the iOS SDK which I filed here: https://github.com/aws-amplify/aws-sdk-ios/issues/3194

Workflow: I implemented custom auth using use case #25 located here: https://github.com/aws-amplify/amplify-js/tree/master/packages/amazon-cognito-identity-js. On the backend, authentication is done through the define/create/verify lambda triggers in cognito. The registration of new users is all done on the server side with admin create user, set password, and linking 3rd party provider accounts.

Problem: When I sign in using cognitoUser.initiateAuth() using an alias, userPool.getCurrentUser().username returns the alias instead of the username. For example, if i signed in with an email alias, userPool.getCurrentUser().username would return the email of the user instead of the actual username. My expectation is that it would always return the username.

harrysolovay commented 4 years ago

This is the expected behavior: when logging in, the username is the supplied unique reference to the user. This can be email address, phone number, or an explicitly-defined username.

If this behavior is undesirable, I'd recommend creating a function which calls userPool.getCurrentUser() and––from its response––distinguishes between and returns the desired username type (for instance, assessing whether the string contains email-only-chars (username.includes("@"))).

SNagappan commented 4 years ago

@harrysolovay When I sign in with an email alias using the standard username/password method, the returned username is actual username and not the email. I'd argue that there is an issue here - standard auth and custom auth are working inconsistently.

harrysolovay commented 4 years ago

Please confirm this is the issue you're having: you're unable to get a consistent reference to the username and email when logging in with different methods?

SNagappan commented 4 years ago

Yes, that is correct. I am unable to get a consistent reference for username with standard and custom auth.