amplify-js: currentSession() returns no current user?
I’m trying to test with the following code in a component that is wrapped by the withAuthenticator() HOC. Federated login works. Once logged in, I get the “No current user” error when running the following code, but the user object is getting populated.
Am I not understanding how the library works? Shouldn’t the currentSession return JWT, acesstoken, id?
Ultimately, I want to be able to get email from Facebook as identity provider.
componentDidMount = () => {
window.LOG_LEVEL = 'DEBUG'
console.log("About to mount")
Auth.currentSession().then(function(session) {
console.log(JSON.stringify(session))
}, function(err) {
console.log(err)
})
Auth.currentAuthenticatedUser().then(function(user) {
console.log("USER: " + JSON.stringify(user))
})
}
Output:
No current user
USER: {"Name":"x x"}
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 18
- Comments: 85 (21 by maintainers)
I’m facing the exact same problem and, I have to say, I feel a little frustrated…
I expected signIn and federatedSignIn to to be only different ways to authenticate the user… I understand user and identity pools have different goals but at this point (as a developer), I just need a simple, unified way to authenticate users, be able to identify them (from the token, server-side) and use services.
PS: @mlabieniec, this issue was closed by inactivity but I think it should be reopened…
From conversations from other issues, one thing is clear to me :
Auth.federatedSignInhas no relation withAuth.currentSession&Auth.currentAuthenticatedUser. When I login via Cognito User Pool and callAuth.currentSession&orAuth.currentAuthenticatedUser, I get JWT token which I can use to access my APIs. But, the response ofAuth.federatedSignInis an object ofCognitoIdentityCredentialsand this object does not have a JWT token. So, my question is how do I get a JWT token when I login usingAuth.federatedSignIn. A lot of the issues on this repo (#699 , #542 , #793 , #703 )ask this question with no definitive answer.@powerful23 The token from
federatedInforeturns the jwt token received from Facebook. I cannot use that to access my APIs in AWS. I get 401 when making API calls I am talking about a jwt token similar to what Congnito provides, that can be used for accessing my APIs in AWS. Referring to the code example from the docs:When using Cognito User Pool
When using Federated SignIn (Facebook example)
Getting the same thing.
currentSessionworks with normal login, but with federated login I getNo current userPlease update your documents to reflect this AWS. Very frustrating.
dont take it as 100% bullet proof, as i am not an AWS employee, this summary is only according to my experience with AWS cognito. TL:DR
Cognito overview:
Recommendation: 1. If you are building a web-app, NOT a mobile app, and doesn’t need advanced user registration / login, the Built-in UI may be good enough for you, and you may benefit from Cognito full solution, having all your users in the user pool, and get the access from federated. 2. If you don’t need to have your own users, and just want to enable social users to get temporary access to AWS resources – you just need federated login. 3. If you don’t want to use the Build-in UI, and want a social login – you will not be able to use the cognito SDK to manage your users, and you will need to have your own solution for one unified users table + basic abilities, example of problematic use cases: a. user registered with email + password, and later on, sign-in with facebook connect, you need to know it is the same user. b. You want to have user id for each user. c. You have a server side, that needs to know who is the currently logged in user, and you don’t want to make it integrate with multiple social services, but with a single users API.
@vsrivatsan89 The
currentSessiononly works when you logged in with Cognito Userpool. We will work on the naming to make it more clear. Thanks!.@kanteankit Your should get an Authorization token from your auth provider. Once you get that token you can do this.
@elorzafe As mentioned in my previous comments,I am familiar with Amazon Cognito and I am already using
Auth.signInfor authenticating via email. That email authentication is creating a user in Cognito User Pool and giving me a JWT token that I can use in Authorization header.The entire point of my question is how can I do a similar thing with
Auth.federatedSignIn. After callingAuth.federatedSignIn, what should I do to get an Authorization token?Note: I cannot use awsmobile-cli.
facing the same problem (logged in with Cognito Userpool) but after changed the amplify configuration from
to
then place it in your app’s entry point i.e. App.js, currentSession() now worked
How to get Cognito Tokens when Authenticating with Social Providers
Auth.federatedSignInin this wayAuth.federatedSignIn()this will open Cognito Hosted UI Web App.Auth.federatedSignIn({provider: 'Facebook'})Auth.federatedSignIn({provider: 'Google'})Auth.federatedSignIn({provider: 'LoginWithAmazon'})Auth.federatedSignIn({provider: 'SignInWithApple'})Then you will have a user on your userpool and Cognito tokens on your App.
Note If you get a token from your Authentication Provider and then use
You wont be able to get a Cognito Tokens and you will No current user error.
Why is this issue closed? We are facing the same problem right now – can’t use graphQL resource with federated signin
@mlabieniec I am experiencing the same issue. You are still not getting what @kanteankit is saying.
We are talking about the JWTOKEN from Cognito. Not the one from the identity provider(eg. Facebook, Google.)
After using Auth.federatedSignIn it only returns the Cognito credentials Obj. So the question is, How do we get the JWTTOKEN from Cognito after the use of Auth.federatedSignIn?
@GenghisJuan you right, it is the most basic - except if u develop a website, and can use the hosted UI. I was also sure that it had to be possible - and if I didn’t got it to work, I must have done something wrong, it took me 5 weeks to give up and share my lesson here in details to save other developers the frustration. (Scroll up if u missed it) 😉 so feel free to waste your time, don’t forget to let us know how was it…
I too have been hacking around here trying to find out a solution. Just to be clear, if I am using react-native + amplify, there is no way to have a user sign up with Facebook and have that user in my Cognito user pools? This is like the most basic use case, hard to believe it isn’t supported.
Facing the same issue:
I’ve created my own identity pool in cognito. For the server/client auth workflow I followed this documentation:
https://docs.aws.amazon.com/cognito/latest/developerguide/developer-authenticated-identities.html
https://docs.amplify.aws/lib/auth/advanced/q/platform/js#identity-pool-federation
Server side code to create the token/identityId:
The result:
With the Client side code, I was able to create the credentials:
This returns no error, and the objects created seems correct:
Credentials:
User:
But despite that I cannot access to any AWS resources yet, like graphql. I keep getting the following error:
No current user
I’m experiencing the same issue. To clear this up when you create an API and add an Authenticator to it, it expects a Token to prove that the user accessing the API came from cognito, i.e. it is safe to provide access.
When you call Auth.currentSession() after a cognito user pool login you are given the following response
CognitoUserSession {idToken: CognitoIdToken, refreshToken: CognitoRefreshToken, accessToken: CognitoAccessToken, clockDrift: 0} accessToken: CognitoAccessToken {jwtToken: "xxxxxxx", payload: {…}} clockDrift: 0 idToken: CognitoIdToken jwtToken: ****[THIS IS USED FOR API CALLS]**** payload: xxxx __proto__: CognitoJwtToken refreshToken: CognitoRefreshToken {token: "xxxxxxxx"} __proto__: ObjectThe idToken->jwtToken is the token needed to authorize API calls.
When Auth.currentSession() is called after a federated sign in, the following is returned :
no current user.How can we get a valid jwt token from a federated sign-in user?
Has there been any updates?
Far out, I can’t believe this is still open with no real resolution. We have got up until
federatedSignInand now need JWT to access the Amplify GraphQL API. Have tried this with both Facebook login and a developer identity.Can someone from AWS confirm that there is no other solution than to create a hosted UI?
@mayteio yes, so the
federatedSignInmethod only works with Cognito Federation Identity Pool to by passing the JWT token from third provider to get a temporary AWS credentials. If you want to do federation with Cognito User Pool and get a JWT token from it, you have to use Cognito Hosted UI.@eyalc4 thanks for your feedback.
Sad to see this issue closed.
I got the same error while using the Authenticator from aws-amplify-react, after logging in with facebook
Auth.currentSession()returnedNo current user.But if I use
Auth.federatedSignIn(),Auth.currentSession()returns an access token that I can use. And I can skip displaying the hosted UI by naming a provider as a parameterAuth.federatedSignIn({provider: 'Facebook'})So do not use the facebook sign in button on the
<Authenticator />component if you want an access token. Do use theAuth.federatedSign({provider: 'provider-name'})to get an access token and skip the UI.Here are some sample code, replace the variables for your user pool with your own:
Thanks @powerful23 though that does not help. It just passes me back the info I passed into the
userargument offederatedSignIn(provider, response, user). No JWT in sight.Thanks @nickadiemus, I thought you made it work without hosted UI, that’s the main issue here. Looks like there is no way of getting AWS token without using hosted UI.
@kanteankit you can get the jwt token by using the
Cache:Sorry we didn’t mention this in the doc, will send a pr for that.
For federate signin & currentSession() to be working,
i would mean “jwt token(accessToken, idToken) & refreshToken returned from the call properly, so those session refresh etc would still be handled by aws-amplify and one can further call essentialCredentials() to call other create service instance later”, like
This is the full approach working for us: giving up approaches federatedSignIn() and UI componenet implementation that are based on hosted ui and craft corresponding oauth link for federated signin. We implemented it for google federated sigin.
On signin page
On the oauth returnUrl, do parsing according to the workaround mentioned in https://github.com/aws-amplify/amplify-js/issues/1316#issuecomment-409290894
I’m using cognito hosted UI and trying to authenticate to appsync. I followed these instructions: https://aws-amplify.github.io/docs/js/react#add-graphql-backend With the same configuration, the only difference is that I have federated auth. I also get a “No current user” error, even though the hosted cognito UI says I’m logged in (via google). As a developer I’d expect once someone signs in via the cognito hosted UI, I’d be able to make appsync requests for my appsync service configured to permit via cognito. The fact someone is signed in via federated identity should not matter.
hey guys, i’m using the Auth.signIn() method which returns a CognitoUser object and among its properties, the signInUserSession has a value of null.
and when i ran
await Auth.currentSession()i got ‘No current user’ as well.if it helps, i’m following a tutorial and this is the step i’m currently on when i encountered this issue.
EDIT: i looked through the properties of the CognitoUser object again and found a property called session
I’m struggling with it, no way without hosted UI… I’m very frustrated!!
@powerful23 Thank you for the prompt response. I think there is some confusion regarding which JWT tokens each of us is talking about.
When doing authentication via
Auth.signIn& callingAuth.currentSessionI get an object ofCognitoUserSessionwhich contains JWT . Now when making an API call I can get the JWT and set the header as follows:In case of federatedSignIn, I can’t figure out how to get this kind of JWT token that can be passed in the header. I get a token from Facebook that I pass along to
Auth.federatedSignInas shown in my previous comment, but, then, what should I do to get an authentication token for making API calls?