如何在 Amplify for Android 中将 Cognito 身份池与 UnAuthenticatd 用户一起使用

Mer*_*ury 5 android amazon-web-services amazon-cognito aws-amplify

我一直在浏览 AWS Amplify文档和教程,了解如何与未经身份验证的用户一起使用 Amplify 和 Cognito 身份池。Amplify 文档给出的示例是:

Amplify.Auth.fetchAuthSession(
    result -> {
        AWSCognitoAuthSession cognitoAuthSession = (AWSCognitoAuthSession) result;
            switch(cognitoAuthSession.getIdentityId().getType()) {
                case SUCCESS:
                    Log.i("AuthQuickStart", "IdentityId: " + cognitoAuthSession.getIdentityId().getValue());
                    break;
                case FAILURE:
                    Log.i("AuthQuickStart", "IdentityId not present because: " + cognitoAuthSession.getIdentityId().getError().toString());
            }
        },
        error -> Log.e("AuthQuickStart", error.toString())
);
Run Code Online (Sandbox Code Playgroud)

但实际上,当我使用此代码时 - 我在 LogCat 中打印出一个错误:

AuthQuickStart: FAILURE IdentityId not present because: AmplifyException {message=You are currently signed out., cause=null, recoverySuggestion=Please sign in and reattempt the operation.}
Run Code Online (Sandbox Code Playgroud)

注意:我确实配置了 AWS Cognito 以支持未经身份验证的用户!

我还到处寻找 Amplify Android API 文档以查看支持哪些其他 API - 找不到任何 Android API 文档。并查看 AWSAmplify.Auth方法,我找不到任何处理未经身份验证的用户的功能

题:

我如何使用 Amplify (Android) 并通过 AWS Cognito 为未经身份验证的用户提供 AWS 凭证?

Dav*_*lin 4

我是 Amplify Android 团队的 David。事实上,我前几天刚刚研究过这个问题,目前需要一个技巧来让未经身份验证的用户正常工作。

通过 CLI 设置 unauth/guest 用户后(正如您所提到的),您必须调用getAWSCredentials底层逃生舱口上的方法一次,应用程序才能使其正常工作。

这是我编写的代码片段,您可以在之后运行Amplify.configure(同样,每次安装应用程序时只需运行一次):

AWSMobileClient mobileClient = (AWSMobileClient) Amplify.Auth.getPlugin("awsCognitoAuthPlugin").getEscapeHatch();

mobileClient.getAWSCredentials(new Callback<AWSCredentials>() {
     @Override
     public void onResult(AWSCredentials result) {

        // Now you'll see the Identity ID and AWSCredentials in the resulting auth session object.
        Amplify.Auth.fetchAuthSession(
            result2 -> Log.i(TAG, result2.toString()),
            error -> Log.e(TAG, error.toString()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
     }

     @Override
     public void onError(Exception e) {
          // Handle the error however is best for your app
     }
});
Run Code Online (Sandbox Code Playgroud)

我现在正在研究一种解决方案来避免这种黑客行为,并在我们的网站上添加有关 Unauth 用户的文档部分,但与此同时,这应该可以让它为您工作。

再次注意,您只需执行一次此操作,从那时起,它应该在您调用时起作用fetchAuthSession

更新:未修补的(官方)版本:

Amplify.Auth.fetchAuthSession(
    result -> {
        AWSCognitoAuthSession cognitoAuthSession = (AWSCognitoAuthSession) result;
        switch (cognitoAuthSession.getIdentityId().getType()) {
            case SUCCESS:
                Log.i(TAG, "identity: " + cognitoAuthSession.getIdentityId().getValue());
                Log.i(TAG, "credentials: " + cognitoAuthSession.getAWSCredentials().getValue(););
                break;
            case FAILURE:
                Log.i(TAG, "FAILURE IdentityId not present because: " + cognitoAuthSession.getIdentityId().getError().toString());
        }
    },
    error -> Log.e(TAG, "UNAUTH USERS ERR: " + error.toString()));
Run Code Online (Sandbox Code Playgroud)