Amplify-js: No current user while I am in app (using aws-amplify-react-native) with Auth.currentSession()

Created on 5 Jun 2020  路  18Comments  路  Source: aws-amplify/amplify-js

Frequently facing No current user while I am in app. Here I am using Auth.currentSession() to get the token and successfully inside the app.

Why the above issue is getting. Any thoughts on this?

Using latest aws-amplify and aws-amplify-react-native .

"aws-amplify": "^3.0.11",
 "aws-amplify-react-native": "^4.1.2",
 "aws-sdk": "^2.681.0",
Can't reproduce React Native pending-close-response-required

Most helpful comment

It is still an issue. Need to reopen the issue

All 18 comments

@nihp Could you provide some reproduction steps so we could replicate this behavior you're seeing?

Just I have launch the app and navigating from a screen to another screen

I have using Authenticator from the aws-amplify-react-native

ConsoleLogger.ts:99 [DEBUG] 53:23.994 AuthClass - currentSession: No current user

yarn list v1.22.4
鈹溾攢 @aws-amplify/[email protected]
鈹溾攢 @aws-amplify/[email protected]
鈹溾攢 @aws-amplify/[email protected]
鈹溾攢 @aws-amplify/[email protected]
鈹溾攢 @aws-amplify/[email protected]
鈹溾攢 @aws-amplify/[email protected]
鈹溾攢 @aws-amplify/[email protected]
鈹溾攢 @aws-amplify/[email protected]
鈹溾攢 @aws-amplify/[email protected]
鈹溾攢 @aws-amplify/[email protected]
鈹溾攢 @aws-amplify/[email protected]
鈹溾攢 @aws-amplify/[email protected]
鈹溾攢 @aws-amplify/[email protected]
鈹溾攢 @aws-amplify/[email protected]
鈹溾攢 @aws-crypto/[email protected]
鈹溾攢 @aws-crypto/[email protected]
鈹溾攢 @aws-crypto/[email protected]
鈹溾攢 @aws-crypto/[email protected]
鈹溾攢 @aws-crypto/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 @aws-sdk/[email protected]
鈹溾攢 [email protected]
鈹溾攢 [email protected]
鈹溾攢 [email protected]
鈹斺攢 [email protected]

Do you have a complete code sample we could look at it? It is just very difficult to debug an issue without having a clear reproduction path.

Here i will took the token from the below code and pass for my API calls to fetch the data. Here while I am in the app doing any fetches it will show the no current user.

AuthClass - Failed to get the current user', 'No current user'

export async function get (endpoint: string, data?) {
    const currentSession = await Auth.currentSession();
    const providerId = currentSession.getIdToken().payload.sub;
    const identityJwt = currentSession.getIdToken().getJwtToken();
    return GET(endpoint, data, identityJwt, providerId);
}

Also I have referred lot of suggestions or solutions for this. But I can't able to rectify this. I need to know what causes this issue and need to debug.

In this thread https://github.com/aws-amplify/amplify-js/issues/3943

>

  1. it could be a chance that your App is calling Auth.currentSession() when the users are logged out or didn't sign in yet?

  2. The error is usually received by processes running on an interval timer after a period of time. We suspect some type of "failed to load resource: net::ERR_NETWORK_IO_SUSPENDED" but this type of error message is 'hidden' by the current code and replaced with "No current User". I think changing the error handling would help others with these type of problems. => My Network connection is good enough

In this thread https://github.com/aws-amplify/amplify-js/issues/2702 they mentioned lot.

  • I had the same problem, but for me, removing the cookie storage configuration in aws-exports.js solved it. Maybe this helps someone. ==> Here I not using the cookie storage
  • My solution is to remove the node_modules and reinstall again. ==> Already tried

  • I had this very same error
    What I realised, is that I needed to run node v8 and that I use nvm to set that version. I think what happened was I ran a node install on the wrong version? Whatever the real cause was, removing node_module, and doing a
    npm use 8; npm install for me fixed it

Your commands might be different, but essentially, removing node_modules, and reinstalling my dependencies is what fixed it

UPDATE: What can also cause this, is your react app calling storage, before the user has been authenticated
==> For this previously we have used nvm 8, but now we using 12. I think it not make any issues.

@nihp It's difficult to replicate the behavior you're seeing without a clear reproduction example. Would you be able to create a basic repo to show the behavior you're seeing and share that with us?

Here I have

Used redux Actions similar to this implementation using aws-amplify-react-native

export default () => {
 return store => {
        Hub.listen('auth', (data) => {
            console.log('Hub.auth event:', data);
            const { payload } = data;
            Auth.currentAuthenticatedUser({ bypassCache: true })
                .then(u => {
                    console.log('Auth.currentAuthenticatedUser result:', u);
                    store.dispatch(user.actions.switchUser({...u}));
                }, err => {
                    store.dispatch(user.actions.switchUser(null));
                });
        });
   return next(action);
}

For communicating with the backend using

export async function get (endpoint: string, data?) {
    const currentSession = await Auth.currentSession();
    const providerId = currentSession.getIdToken().payload.sub;
    const identityJwt = currentSession.getIdToken().getJwtToken();
    return GET(endpoint, data, identityJwt, providerId);
}

aws-exports.js

const awsmobile = __DEV__ ? {
    "aws_project_region": "",
    "aws_cognito_identity_pool_id": "",
    "aws_cognito_region": "",
    "aws_user_pools_id": "",
    "aws_user_pools_web_client_id": "",
    "oauth": {},
    "aws_user_files_s3_bucket": "",
    "aws_user_files_s3_bucket_region": ""
} : {
    "aws_project_region": "",
    "aws_cognito_identity_pool_id": "",
    "aws_cognito_region": "",
    "aws_user_pools_id": "",
    "aws_user_pools_web_client_id": "",
    "oauth": {},
    "aws_user_files_s3_bucket": "",
    "aws_user_files_s3_bucket_region": ""
}

Implemented as

 Auth.signIn(username, password)
    .then(user => console.log(user))
      .catch(err => console.log(err));
})
import {
  Authenticator,
  AmplifyTheme,
  Greetings,
  SignIn,
  ConfirmSignIn,
  RequireNewPassword,
  SignUp,
  ConfirmSignUp,
  VerifyContact,
  ForgotPassword,
  AuthPiece,
  withAuthenticator,  FormField, LinkCell, Header, ErrorRow, AmplifyButton
} from "aws-amplify-react-native";

  <Authenticator
          hideDefault ={true}
          amplifyConfig={awsmobile}
          theme={this.state.theme}
          signUpConfig={signUpConfig}
          onStateChange={this.onStateChange}
        >
          <CustomizedSignIn  override={'SignIn'}/>
          <CustomizedSignUp  signUpConfig={signUpConfig} override={'SignUp'}/>
          <CustomizedForgotPassword override={'ForgotPassword'}/>
          <CustomizedConfirmRegistration override={'ConfirmSignUp'}/>
          <ConfirmizedSignInScreen override={'ConfirmSignIn'}/>
          <Loading/>
        </Authenticator>

Any updates?

export async function get (endpoint: string, data?) {
    const currentSession = await Auth.currentSession();
    const providerId = currentSession.getIdToken().payload.sub;
    const identityJwt = currentSession.getIdToken().getJwtToken();
    return GET(endpoint, data, identityJwt, providerId);
}

<Authenticator hideDefault ={true} amplifyConfig={awsmobile} theme={this.state.theme} signUpConfig={signUpConfig} onStateChange={this.onStateChange} > <CustomizedSignIn override={'SignIn'}/> <CustomizedSignUp signUpConfig={signUpConfig} override={'SignUp'}/> <CustomizedForgotPassword override={'ForgotPassword'}/> <CustomizedConfirmRegistration override={'ConfirmSignUp'}/> <ConfirmizedSignInScreen override={'ConfirmSignIn'}/> <Loading/> </Authenticator>

A couple of thoughts:

  • Is this get function ever called while the user is not logged in? If so, Auth.currentSession() would throw the error you are seeing. Try adding some logging in that function to see when it is actually being called.

  • I don't see your actual app inside <Authenticator>. Did you leave it out just for the example, or is it rendered somewhere else?

  • Is this get function ever called while the user is not logged in? If so, Auth.currentSession() would throw the error you are seeing. Try adding some logging in that function to see when it is actually being called

Auth.currentSession() only called while am in the app. After logout it not get called for sure.

  • I don't see your actual app inside <Authenticator>. Did you leave it out just for the example, or is it rendered somewhere else?

Authenticator.js is a screen it is handled in the navigation of my app.

My question is I am in my app. (i.e) Any of the screens inside the app after successful login. Suddenly getting No current user error.

Would it be possible to create a full working basic example repo or Expo Snack to show the problem you are seeing so we can actually run the app to replicate the behavior?

Did you need full app code to debug this issue.

If you could create a small example that replicates the issue you are seeing, that would be great!

I have used the customized screen with existing or default screens.

Also, the above GET call code makes interaction with backend and aws-amplify.

My code structure is like below

Snack expo: https://snack.expo.io/@niph/auth

Thanks for that @nihp! However, is there a way you could make that example a bit more complete? It doesn't need to be your exact code, but just a simplified version of it that can actually run to show the behavior you're seeing. The key here is just providing us a complete reproduction sample so we can determine if there's a bug or documentation that needs to be updated.

You can keep the aws-exports file empty, and I'll replace it with my own aws-exports file when running the example.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

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

It is still an issue. Need to reopen the issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ddemoll picture ddemoll  路  3Comments

callmekatootie picture callmekatootie  路  3Comments

romainquellec picture romainquellec  路  3Comments

rygo6 picture rygo6  路  3Comments

josoroma picture josoroma  路  3Comments