Amplify-js: Auth.currentAuthenticatedUser() always return "Not Authenticated" causing Infinite reloads

Created on 23 Mar 2020  Â·  20Comments  Â·  Source: aws-amplify/amplify-js

* Which Category is your question related to? *
Correct implementation of Auth.currentAuthenticatedUser()
* What AWS Services are you utilizing? *
Cognito
* Provide additional details e.g. code snippets *
I have a use-case wherein, I first want to check if a user is authenticated or not on the first visit to the site. If the user is not authenticated, I want to redirect the user to the OIDC provider configured at Cognito using Auth.federatedSignIn(). However, I am getting "Not Authenticated" response from the Auth.currentAuthenticatedUser() everything.

This is the function calling Auth.currentAuthenticatedUser():

export function getCurrentUser() {
  console.log("inside getCurrentUser try");
  Auth.currentAuthenticatedUser()
    .then(user => {
      console.log(user);
      return user;
    })
    .catch(ex => {
      console.log(ex);
      console.log("inside getCurrentUser catch, calling federatedSignIn");
      Auth.federatedSignIn({ provider: "Federate" });
    });
}

Now, from the component, I am calling getCurrentUser()

  componentDidMount() {
    try {
      userData = auth.getCurrentUser();
      console.log("I am the data" + userData);
      if (userData) {
        this.props.onAddUserData();
        this.setState({ isLoggedIn: true });
      }
    } catch (ex) {
      console.log(ex);
      console.log("Not logged in. Redirect to OIDC");
    }
  }

The events I am getting in the console:
1) inside getCurrentUser try
2) I am the data undefined
3) not authenticated
4) inside getCurrentUser catch, calling federatedSignIn

The page is getting refreshed again and again.

OAuth pending-close-response-required question

Most helpful comment

Why this issue has been closed? Is there any update on this issue? I am also facing same issue.

All 20 comments

see https://github.com/aws-amplify/amplify-js/issues/4420. if doesn't help, please provide your amplify-js version

This issue has been automatically closed because of inactivity. Please open a new issue if are still encountering problems.

Same for me, can't bring the user data, the error message it's probably wrong as well

"aws-amplify": "^3.0.5",
"aws-amplify-react": "^4.1.4",
"aws-appsync": "^3.0.2",

Why this issue has been closed? Is there any update on this issue? I am also facing same issue.

"aws-amplify": "^3.0.16",
"expo": "~37.0.9",
"react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",

Same problem, this needs to be reopened.

please reopen this issue. Was anyone able to solve it?

I'm looking for a solution too

Can't figure this out either. Happens on the browser.

I literally call:

const user = await Auth.signIn(email, password);
console.log(user); // ✅  is correct
const response = await Auth.currentAuthenticatedUser();
// 🚫  throws 'not authenticated'

To make sure the user is actually full authenticated:

  • Go to the AWS dashboard and click on Cognito ( make sure you are in the correct region )
  • Click on Manage User Pools
  • Go to users and user groups ( left menu )

There you will see the list of users with the email verified or not, remember the user is full verified once they have entered the verification code sent by email

Screen Shot 2020-08-12 at 22 12 57

Screen Shot 2020-08-12 at 22 13 28

Screen Shot 2020-08-12 at 22 13 54

After you sign up a user, I believe you need to also sign in. For example:

  1. Auth.signUp
  2. Auth.confirmSignUp
  3. Auth.signIn

I'm facing the same problem, here. Did anyone find a solution?

{
    "amazon-cognito-identity-js": "^4.3.5",
    "aws-amplify": "^3.0.24",
    "aws-amplify-react-native": "^4.2.5",
    "react": "16.11.0",
    "react-native": "0.62.2"
}

We are also facing this issue.
For whatever reason the promise resolves with a "not authenticated" value instead of rejecting.

I'm 99% sure this is some sort of botched localStorage lookup because currentAuthenticatedUser isn't even doing any sort of XHR request.

I'm having this issue as as well, in my case i created the user using admin_create_user with aws-sdk for ruby

I am also facing this issue. For my react routes, I am checking the authentication and it always returns not authenticated.

@kvdy Maybe you are missing the confirmation step? _Auth.confirmSignUp_

In my case I had MFA turned on and the Auth module was waiting for OTP. I made MFA optional for my user pool and all went OK

I'm also facing this issue, I'm using React and it returns "not Authenticated" everytime. I am manually creating users in the user pool and marking the email as "verified" and I still encounter the problem. Is there a solution?

Also facing the same issue. User is verified and confirmed. I can see that in the developer tool, amplify is sending proper code challenge and getting access token in return. The problem is, as the @TheDutchCoder said, user data is not getting persisted in the storage. Further debugging shows that the Hub.listen('auth') is not fired from_handleAuthResponse function in node_modules\@aws-amplify\auth\lib\Auth.js, which in turn was not fired after getting back from the login. This is very weird.

I remembered that this infinite reload might happen if the user you are using is not "confirmed" using the phone or email code

I was getting similar errors using a Cognito pool configured with an ADFS identity provider and amplify-3.8.8. I was able to get it working with the following config:

When using a custom identity provider - i.e. not one of the pre-baked [ COGNITO | Google | Facebook | LoginWithAmazon | SignInWithApple ] providers, the object passed to Auth.federatedSignIn() should be:
{ customProvider: "CustomProviderName" }
where CustomProvidernName is as configured in the Cognito pool

Additionally, ensure the oauth entry in the object passed to Auth.configure() (from aws-exports.js) includes the following:

{
  ...
  oauth: {
    ...
    scope: ["openid", "email"],
    responseType: "code",
  }
}

The Cognito user pool setup section of the docs has an example of appending to the config.

See also this SO question

Was this page helpful?
0 / 5 - 0 ratings