简体   繁体   中英

AWS Amplify Auth - Error when fetching user attributes of a signed in user

I'm trying to use AWS Amplify Auth in my Android App. Sign Up and Sign In works, now I'm trying to fetch the User Attributes of the signed in user, but Amplify throws an exception in the Amplify.Auth.fetchUserAttributes() function.

My sign in code:

private void login(String email, String password) {
        Amplify.Auth.signIn(
                email,
                password,
                result -> {
                    if (result.isSignInComplete()) {
                        Log.i(TAG, "Sign in succeeded");
                        Log.i(TAG, "going to fetch User attributes");
                        
                            Amplify.Auth.fetchUserAttributes(
                                    attributes -> Log.i("AuthDemo", "User attributes = " + attributes.toString()),
                                    error -> Log.e("AuthDemo", "Failed to fetch user attributes.", error)
                            );

                        Log.i(TAG, "end of fetching User attributes");
                    } else {
                        Log.i(TAG,  "Sign in not complete");
                    }

                },
                error -> Log.e(TAG, error.toString())
        );
    }

The log:

I/LoginActivity: Sign in succeeded
W/InternalCallback: Library attempted to call user callback twice, expected only once
I/LoginActivity: going to fetch User attributes
D/AWSMobileClient: Inspecting user state details
D/AWSMobileClient: waitForSignIn: userState:SIGNED_OUT
E/AuthDemo: Failed to fetch user attributes.
    AuthException{message=Failed to fetch user attributes, cause=java.lang.Exception: Operation requires a signed-in state, recoverySuggestion=Ensure that you are logged in and online}
        at com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin$15.onError(AWSCognitoAuthPlugin.java:736)
        ....
     Caused by: java.lang.Exception: Operation requires a signed-in state
        ...

It tells me that the User state is: SIGNED_OUT, but I'm calling it inside the successful sign in method, so why is the user signed out?

Please check if your identity poolId is correct (even if you're not using a guest access) in app -> src -> res -> raw -> amplifyconfiguration.json & awsconfiguration.json . You can find your poolId in your AWS console -> cognito -> manage identity pools -> click on your pool -> click on Sample code:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM