amazon-archives / amazon-cognito-auth-js

The Amazon Cognito Auth SDK for JavaScript simplifies adding sign-up, sign-in with user profile functionality to web apps.
Apache License 2.0
423 stars 232 forks source link

getCurrentUser() returning null for facebook user on the redirected page. #62

Open joe455 opened 6 years ago

joe455 commented 6 years ago

I am able to save the facebook user into userpool but when I am trying to retrive the current user on the redirected page, it's giving null always.Can somebody tell me why this is happening?

yuntuowang commented 6 years ago

Hi @joe455, are you calling getCurrentUser() in your onSuccess() callback?

joe455 commented 6 years ago

Nope. I didn't understood this one yet I guess. I will walk through with my steps. I am developing this app in angular4.

Could you please let me what I did wrong here?

yuntuowang commented 6 years ago

@joe455 As you described, you try to call getCurrentUser() once you signed in successfully and are redirected to the myprofile page, which is correct. I am not entirely sure why it gives the null.

joe455 commented 6 years ago

So basically it should give me the current user details as a cognito user on the redirected page. Isn't correct?

joe455 commented 6 years ago

@yuntuowang auth.signOut() is not logging out the facebook.

yuntuowang commented 6 years ago

"So basically it should give me the current user details as a cognito user on the redirected page. Isn't correct?" @joe455 Yes, you are correct.

joe455 commented 6 years ago

OMG..Then why it's giving me null..I will look into this.

yuntuowang commented 6 years ago

Hi @joe455, I think the reason you getting null is cause of a known bug from us. Currently the signed in user is not updated correctly. Basically when caching tokens and scopes, the username should be updated to username getting from id token. We will push this bug fix really soon. Will post on this issue once we pushed, thanks!

yuntuowang commented 6 years ago

Hi @joe455, our bug fix has been pushed! Please use version v1.1.1, you can download directly from github or use npm to install the latest version. Thanks!!

kawsark commented 6 years ago

Hello @joe455 or @yuntuowang, how can I call getCurrentUser() with the Cognito Auth API? It seems to be part of the Cognito Identity API and I am not sure if both APIs can coexist.

I can use the hosted UI and login, however in the onSuccess handler, I need to obtain information about the current user. I am using Cognito User Pool with Implict Grant flow. In the onSuccess handler I have access to the session and CognitoAuth objects.

yuntuowang commented 6 years ago

Hi @kawsark, you need to call getCurrentUser() method(which is defined in this SDK, Line 151 in CognitoAuth.js file), it is not the Cognito Identity API, has nothing to do with the Cognito Identity service. It may be confusing since they have the same name.

So in your onSuccess handler, you need to call this getCurrentUser() method and get the info about the current signed in user.

Thanks a lot! Let me know if you have any other questions.

joe455 commented 6 years ago

@yuntuowang I was calling getCurrentUser() method on identity JS SDK till now. Thanks for clarifying this. I will give a try on this.

yuntuowang commented 6 years ago

Hi @joe455, sorry for the confusion. Basically we need to use accurate wording. The Auth SDK only provides two APIs, getSession() and signOut().

You can also use all the other methods defined in the SDK, e.g. getCurrentUser() method. All of these methods has nothing to do with other SDKs.

kawsark commented 6 years ago

Thank you @yuntuowang , I was able to retrieve the username using getCurrentUser(). To retrieve some of the attributes specified during signup (email, Name etc.), I assume the Cognito Identity SDK is needed? The cognitoUser.getUserAttributes() function doesn't seem to exist in this API. Let me know if there is a different way to retrieve the attributes. I can't import both amazon-cognito-auth.js and amazon-cognito-identity.js in the same .js file.

yuntuowang commented 6 years ago

Hi @kawsark, yes, getCurrentUser() only gives you the current signed in user's username.

Here is a great example about how to use Cognito User Pools and Cognito Identity with this SDK and the Cognito ID SDK in a vanilla create-react-app: https://gist.github.com/gabeweaver/d1be9f0d41069437f576c375c30e134c

yuntuowang commented 6 years ago

Hi @kawsark, to retrieve some of the attributes specified during signup (email, Name etc.), you can parse the id_token returned by cognito once you signed in (you have set the 'openid' as a scope, or you set nothing for scope('openid' is the default setting)).