[英]How to implement signOut once signed in with Cognito/UserPool on Android
[英]AWS Cognito SignOut Android
我正在使用适用于 Android 的最新版 AWS 开发工具包。
implementation 'com.amazonaws:aws-android-sdk-core:2.7.6'
implementation 'com.amazonaws:aws-android-sdk-cognito:2.7.6'
implementation 'com.amazonaws:aws-android-sdk-cognitoidentityprovider:2.7.6'
我的身份验证处理程序大部分取自他们的示例代码。
// create a handler for the sign-in process
private AuthenticationHandler authenticationHandler = new AuthenticationHandler() {
@Override
public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) {
// String idToken = userSession.getIdToken().getJWTToken();
// Map<String, String> logins = new HashMap<>();
// logins.put("cognito-idp.us-east-1.amazonaws.com/" + getString(R.string.user_pool_id), idToken);
// AuthHelper.getInstance().getCredentialsProvider().setLogins(logins);
//
// new RefreshCognitoCredentials().execute();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}
@Override
public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) {
String password = inputPassword.getText().toString();
AuthenticationDetails authenticationDetails = new AuthenticationDetails(userId, password, null);
authenticationContinuation.setAuthenticationDetails(authenticationDetails);
authenticationContinuation.continueTask();
}
@Override
public void getMFACode(MultiFactorAuthenticationContinuation continuation) {
}
@Override
public void authenticationChallenge(ChallengeContinuation continuation) {
}
@Override
public void onFailure(Exception exception) {
String error = AuthHelper.formatException(exception);
layoutUsername.setErrorEnabled(true);
layoutUsername.setError(error);
}
};
身份验证工作得很好。 并且它应该缓存。 在我的启动画面活动中,我可以检查 CognitoUser.getCurrentUser().getUserId()。
现在注销:
CognitoUser.getCurrentUser().signOut()
现在,如果我关闭应用程序并打开应用程序 - CognitoUser.getCurrentUser().getUserId() 仍然返回我以前登录的用户。
几个月前我用 2.2.+ 作为我的 sdk 版本完成了一个 AWS 实现,这个例子按预期工作。
注意* 如果我尝试 CognitoUser.getCurrentUser().globalSignout() - 它会返回“用户未通过身份验证”错误。
如果我有一个有效的用户/会话,我如何检查应用程序的启动? 我讨厌 AWS 如何在没有文档或无法找到的文档的情况下每天更改内容。
signOut
从 SharedPreferences 中清除缓存的令牌。 它清除存储在 access、id 和 refresh 令牌下的数据。 但是,LastAuthUser 键包含未由 signOut 清除的用户 ID。
当您调用cognitoUserPool.getCurrentUser().getUserId()
,它会检查 SharedPreferences 中是否存在 LastAuthUser 键,因此它会返回 userId。 我正在调查这个问题。 当我可以确认预期的行为时,将更新此答案。
我在更新用户的电子邮件时遇到过这种情况。 就我而言,当用户切换回以前使用的电子邮件时,cognito 不会更新最后一个经过身份验证的用户的值。
我的解决方案是手动更新共享首选项中的值,其中 cognito 更新最后一个经过身份验证的用户:
SharedPreferences.Editor editor = context.getSharedPreferences("CognitoIdentityProviderCache", 0).edit();
String csiLastUserKey = "CognitoIdentityProvider." + cognitoUserPool.getClientId() + ".LastAuthUser";
editor.putString(csiLastUserKey, newEmail);
editor.commit();
这是一个hacky的解决方法,因为我正在强制cognito应该自动执行什么操作。
我有几乎同样的问题。 我的步骤:
1. Sign in as user 1
2. Sing out
3. Sign in as user 2
4. Surprise that user 1 is signed in instead of user 2
所以,决定升级到新的SDK
com.amazonaws:aws-android-sdk-mobile-client:2.13.4
它使用
implementation 'com.amazonaws:aws-android-sdk-core:2.13.4'
implementation 'com.amazonaws:aws-android-sdk-cognito:2.13.4'
implementation 'com.amazonaws:aws-android-sdk-cognitoidentityprovider:2.13.4'
但它没有帮助。
与 /data/data/applicationId/shared_prefs/CognitoIdentityProviderCache.xml 中的 CognitoIdentityProviderCache.xml 文件相关的问题
注销后不会清除 CognitoIdentityProviderCache.xml 文件。
退出示例后的文件内容:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="CognitoIdentityProvider.app_id.LastAuthUser.encrypted">...</string>
<string name="CognitoIdentityProvider.app_id.LastAuthUser.encrypted.iv">...</string>
<string name="CognitoIdentityProvider.app_id.LastAuthUser.encrypted.keyvaluestoreversion">1</string>
</map>
从文件中删除这 3 行可解决此问题。
解决方法:
但是文件名可以在下一个版本中更改。
与在系统设置 -> 应用 -> AwesomeApp 中按“清除数据”相同
context.cacheDir.parentFile.deleteRecursively()
您可以在他们的错误跟踪器中关注我的错误报告以获取详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.