我正在使用最新版本的AWS SDK for Android。
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);
}
};
身份验证工作正常。它应该缓存它。在我的splashscreen活动中,我能够检查CognitoUser.getCurrentUser()。getUserId()。
现在注销:
CognitoUser.getCurrentUser().signOut()
现在,如果我关闭应用程序并打开应用程序 - CognitoUser.getCurrentUser()。getUserId()仍会返回我以前登录的用户。
几个月前我用2.2。+作为我的sdk版本完成了AWS实现,这个例子按预期工作。
注意*如果我尝试使用CognitoUser.getCurrentUser()。globalSignout() - 它将返回“用户未经过身份验证”错误。
如果我有有效的用户/会话,我该如何检查应用启动?我讨厌AWS如何每天更改内容,没有无法找到的文档或文档。
signOut
从SharedPreferences中清除缓存的令牌。它清除访问,id和刷新令牌下存储的数据。但是,LastAuthUser键包含signOut未清除的用户ID。
当您调用cognitoUserPool.getCurrentUser().getUserId()
时,它会检查SharedPreferences中是否存在LastAuthUser密钥,因此它返回userId。我正在研究这个问题。当我能确认预期的行为时,会更新这个答案。