英文:
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论