Amplify身份验证事件在安卓设备上无法正常工作。

huangapple go评论88阅读模式
英文:

Amplify Auth events don't work in android

问题

我正在开发一个使用AWS Amplify库进行身份验证的应用程序。
我尝试通过查看Amplify的身份验证事件来检查登录状态,但是它不起作用。
我尝试通过logcat进行验证,但它只显示了获取输出。

Amplify.Auth.fetchAuthSession(
    result -> Log.i("AmplifyQuickstart", result.toString()),
    error -> Log.e("AmplifyQuickstart", error.toString())
);

Amplify.Hub.subscribe(HubChannel.AUTH, hubEvent -> {
    if (hubEvent.getName().equals(InitializationStatus.SUCCEEDED.toString())) {
        Log.i("AuthQuickstart", "身份验证成功初始化");
    } else if (hubEvent.getName().equals(InitializationStatus.FAILED.toString())) {
        Log.i("AuthQuickstart", "身份验证未能成功初始化");
    } else {
        switch (AuthChannelEventName.valueOf(hubEvent.getName())) {
            case SIGNED_IN:
                Log.i("AuthQuickstart", "刚刚完成登录。");
                break;
            case SIGNED_OUT:
                Log.i("AuthQuickstart", "刚刚登出。");
                break;
            case SESSION_EXPIRED:
                Log.i("AuthQuickstart", "身份验证会话刚刚过期。");
                break;
            default:
                Log.w("AuthQuickstart", "未处理的身份验证事件:" + AuthChannelEventName.valueOf(hubEvent.getName()));
                break;
        }
    }
});
英文:

I'm working in on an app that uses authentication from the AWS Amplify library.
I tried to check the sign-in state by looking at Amplify's auth events, but it
doesn't work. I tried to verify with logcat, but it only shows me the output of
the fetch.

Amplify.Auth.fetchAuthSession(
    result -> Log.i("AmplifyQuickstart", result.toString()),
    error -> Log.e("AmplifyQuickstart", error.toString())
);

Amplify.Hub.subscribe(HubChannel.AUTH, hubEvent -> {
    if (hubEvent.getName().equals(InitializationStatus.SUCCEEDED.toString())) {
        Log.i("AuthQuickstart", "Auth successfully initialized");
    } else if (hubEvent.getName().equals(InitializationStatus.FAILED.toString())) {
        Log.i("AuthQuickstart", "Auth failed to succeed");
    } else {
        switch (AuthChannelEventName.valueOf(hubEvent.getName())) {
            case SIGNED_IN:
                Log.i("AuthQuickstart", "Auth just became signed in.");
                break;
            case SIGNED_OUT:
                Log.i("AuthQuickstart", "Auth just became signed out.");
                break;
            case SESSION_EXPIRED:
                Log.i("AuthQuickstart", "Auth session just expired.");
                break;
            default:
                Log.w("AuthQuickstart", "Unhandled Auth Event: " + AuthChannelEventName.valueOf(hubEvent.getName()));
                break;
        }
    }
});

答案1

得分: 0

以下是翻译好的部分:

一种我成功解决该问题的方法是将错误 lambda 引用传递给 errorAuthenticationHandle 函数,如下所示:

Amplify.Auth.signIn(this.userName, password,
    result -> Log.i("AuthQuickstart", result.isSignInComplete() ?
        "登录成功" : "登录未完成"),
    this::errorAuthenticationHandle
);

private void errorAuthenticationHandle(AuthException error) {
    Log.e("AmplifyQuickstart", error.toString());

    switch (getErrorNumber(error)) {
        // 服务器要求重置密码
        case RESET:
            // 一些代码
            break;

        case FAILED:
            // 一些代码
            break;
    }
}
英文:

One way that I succeed to handle the problem is to pass the error lambda
reference to errorAuthenticationHandle function as follows:

Amplify.Auth.signIn(this.userName, password,
    result -> Log.i("AuthQuickstart", result.isSignInComplete() ?
        "Sign in succeeded" : "Sign in not complete"),
    this::errorAuthenticationHandle
);

private void errorAuthenticationHandle(AuthException error) {
    Log.e("AmplifyQuickstart", error.toString());

    switch (getErrorNumber(error)) {
        // Server require password reset
        case RESET:
            // Some code
            break;

        case FAILED:
            // Some code
            break;
    }
}

huangapple
  • 本文由 发表于 2020年8月25日 11:53:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63571815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定