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() return different platform results when using email based auth #201

Closed
kjones opened this issue Nov 20, 2020 · 15 comments
Labels
auth Issues related to the Auth Category bug Something is not working; the issue has reproducible steps and has been reproduced fixed-in-release-candidate Issues that have been addressed in the current release-candidate branch iOS Issues specific to the iOS Platform platform-discrepancy Issues that result in inconsistent behavior between support platforms requires-ios-fix This issue is the result of an underlying Amplify iOS issue that needs to be fixed.

Comments

@kjones
Copy link

kjones commented Nov 20, 2020

When adding Cognito auth to a project, if email based sign-in is chosen (might apply to phone number also) then Amplify.Auth.getCurrentUser() return different AuthUser results between Android and iOS. Android returns with AuthUser.username set to the email address but iOS appears to return the Cognito userSub value.

To Reproduce

  1. Configure Amplify Auth (amplify add auth) to use email based authentication.
    Screen Shot 2020-08-23 at 6 56 29 PM
  2. Sign-up, confirm, and Sign-in.
  3. At some point later in time, call Amplify.Auth.getCurrentUser().

Expected behavior
Amplify.Auth.getCurrentUser() should return the same results on both Android and iOS.

Platform
Amplify Flutter 0.0.1-dev.3
[x] Android
[x] iOS

@kjones
Copy link
Author

kjones commented Nov 20, 2020

Current workaround to get email on both platforms.

final authState = await Amplify.Auth.fetchAuthSession(
        options: CognitoSessionOptions(getAWSCredentials: true))
    as CognitoAuthSession;
if (authState.isSignedIn) {
  final claims = _parseJwt(authState.userPoolTokens.idToken);
  final email = claims['email'] as String;
}

_parseJwt() borrowed from here: https://stackoverflow.com/a/52021206/2929266

@haverchuck haverchuck added auth Issues related to the Auth Category bug Something is not working; the issue has reproducible steps and has been reproduced labels Nov 21, 2020
@haverchuck haverchuck self-assigned this Nov 21, 2020
@srimajji
Copy link

@kjones I noticed this as well and thank you for the workaround!

@fpabl0
Copy link

fpabl0 commented Nov 27, 2020

@kjones yes, you are right. However, it seems to be problem of the native libraries, they have different implementation on that part. Just to note, iOS implementation is correct as the username has to be different from the email, the mistake is on the Android native library.

@haverchuck
Copy link
Contributor

@kjones We have an API that should allow you to fetch the user attributes, so parsing the JWT shouldn't be necessary:

await Amplify.Auth.fetchUserAttributes()

@nguyenVanQuocKhanh
Copy link

The following code correctly obtains user's email in both iOS and Android:
Future _usersEmail() async {
try {
var attributes = (await Amplify.Auth.fetchUserAttributes()).toList();
for (var attribute in attributes) {
if (attribute.userAttributeKey == 'email') {
print("user's email is ${attribute.value}");
return '${attribute.value}';
}
}
return 'no email';
} on AuthException catch (e) {
return '${e.message}';
}
}

@Jordan-Nelson
Copy link
Contributor

It looks like this is a result of an issue in the amplify-ios library used by amplify-flutter when targeting iOS. There is an issue open in the amplify-ios repo currently here: aws-amplify/amplify-swift#1255. I will follow up with the team and see if this is being looked into.

As several others have already noted, you can get the email address consistently on both platforms using Amplify.Auth.fetchUserAttributes() as a work around.

@Jordan-Nelson Jordan-Nelson added iOS Issues specific to the iOS Platform requires-ios-fix This issue is the result of an underlying Amplify iOS issue that needs to be fixed. labels Jun 8, 2021
@Jordan-Nelson Jordan-Nelson added the platform-discrepancy Issues that result in inconsistent behavior between support platforms label Jun 9, 2021
@julien-tamade
Copy link

Anybody know how to do the opposite, get the username on both instead of email?

@offlineprogrammer offlineprogrammer added this to Pending Triage in Issues Triaging via automation Aug 27, 2021
@offlineprogrammer offlineprogrammer moved this from Pending Triage to Waiting for iOS\Android fix in Issues Triaging Aug 27, 2021
@guplem
Copy link

guplem commented Mar 3, 2022

Does this happen as well with userId? I do not have the means to test it.

I am using it to store user-related data, and now I am worried... Should I use another method to say, for example, that a note has been created by a user?

I am using this value:
Amplify.Auth.getCurrentUser().then((value) => print("USER: " + value.userId));

@dnys1
Copy link
Contributor

dnys1 commented Mar 3, 2022

@guplem - The user ID will always be the Cognito identifier. It seems there is a discrepancy around username in some cases, but the user ID will not be affected in the same way.

@ebo8
Copy link

ebo8 commented Apr 7, 2022

Anybody know how to do the opposite, get the username on both instead of email?

I'd like to know this as well.

Currently on iOS you can get the user's username with:

AuthUser user = await Amplify.Auth.getCurrentUser();
return user.username;

But on Android it returns the email address if the user signed in with email.

@Jordan-Nelson
Copy link
Contributor

Hello everyone - We have recently released a new developer preview version of the Auth category (amplify_flutter v1.0.0-next.0). This new version is a full re-write of the auth category in dart, and it supports Mobile, Desktop, and Web. This issue is addressed in this new version. You can read more about the developer preview release in the blog post and docs. I will update this issue again once this new version is out of developer preview.

Let me know if you have any questions!

@Jordan-Nelson Jordan-Nelson added the pending-release Issues that have been addressed in main but have not been released label Aug 2, 2022
@Jordan-Nelson Jordan-Nelson added the fixed-in-release-candidate Issues that have been addressed in the current release-candidate branch label Mar 2, 2023
@Jordan-Nelson Jordan-Nelson removed the pending-release Issues that have been addressed in main but have not been released label Mar 2, 2023
@Jordan-Nelson
Copy link
Contributor

This issue has been addressed in v1.0.0 of Amplify Flutter, which is now stable. This release also includes web and desktop support for Auth, API, Analytics, and Storage. You can see the list of new features and bug fixes in the release notes, and see more details on how to migrate in the upgrade guide.

Issues Triaging automation moved this from Waiting for iOS\Android fix to Closed Apr 17, 2023
@jniemer
Copy link

jniemer commented Jun 25, 2023

Hello. I am using Amplify 1.2.0 and this is still an issue. AuthUser returned by Amplify.Auth.getCurrentUser() has the user id as userId and username. I am using email based authentication on Android.

@alterhuman
Copy link

Still have this issue. Getting id on iOS instead of email. Using v1.3.0.

@dnys1
Copy link
Contributor

dnys1 commented Aug 14, 2023

Hi @alterhuman, please see my comment here and let me know if that resolves your issue.

When an issue is closed, it's hard for us to gain visibility into comments which are added. Please always feel free to open a new issue so that we can better assist you.

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 bug Something is not working; the issue has reproducible steps and has been reproduced fixed-in-release-candidate Issues that have been addressed in the current release-candidate branch iOS Issues specific to the iOS Platform platform-discrepancy Issues that result in inconsistent behavior between support platforms requires-ios-fix This issue is the result of an underlying Amplify iOS issue that needs to be fixed.
Projects
No open projects
Development

No branches or pull requests