我试图弄清楚如何使用AWS Cognito登录用户。 tutorials似乎都是从注册用户的角度来处理用户,而不是签署用户。我不希望用户通过注册过程;这将由我们的办公室用户在其他地方完成。我只想在这个应用程序中拥有一个流程,让他们输入现有的用户名和密码并登录。
我目前对事物的理解是,Cognito用户池仅支持使用Facebook或Google等身份验证提供程序登录,或者使用非身份验证登录,我无法确定是否使用用户名和密码(I在任何情况下都找不到该流程中的任何地方提供用户名和密码。还有Cognito Federated Identities,它似乎也被称为Cognito User Pools的一半时间,它有上述注册教程,但没有关于只是登录现有用户。
我是否必须使用用户池的联合身份版本才能使用用户名和密码登录?如果没有,我如何使用非联合用户池执行此操作?如果是这样,我如何只是为了登录而不注册?我试图抓住上述教程中看起来很相似的部分,但是我感到沮丧,因为我已经在这几周追逐我的尾巴了,只有更多层次的东西依赖于依赖于其他东西的其他东西在望。
流程如何工作有点令人困惑。正如@Ionut Trestian解释的那样,我们需要从池中创建看似空白的用户,然后对该用户进行身份验证。 API已经改变了一些,这是更新的方法。
CognitoUserPool userPool = new CognitoUserPool(context, userPoolId, clientId, clientSecret, region;
//OR if using awsconfiguration.json
// CognitoUserPool userPool = new CognitoUserPool(context, AWSMobileClient.getInstance().getConfiguration());
AuthenticationDetails authDetails = new AuthenticationDetails(username, password, null);
CognitoUser user = userPool.getUser();
//You might want do to the following bit inside a thread as it should be done in background
user.initiateUserAuthentication(authDetails, authHandler, true).run();
Cognito用户池似乎就是您想要的应用程序。 Cognito用户池的功能是为您提供存储用户属性数据的用户目录,并可用于通过您的移动应用/网站对用户名和密码进行身份验证。
通过Cognito Federated Identities,您可以联合Facebook,Google甚至上面的Cognito用户池中的用户,以获取AWS凭据以访问AWS资源。
从您的用例看,您似乎想要从管理员端创建和确认用户,这是Cognito通过使用adminCreateUser API提供的功能。之后,用户可以使用您链接的教程中的示例6,使用用户名和密码登录。
您可以通过在初始化的UserPool上调用getUser()来创建一个空的CognitoUser。
码:
user = userPool.getUser();
AuthenticationDetails authenticationDetails = new AuthenticationDetails(email, password, null);
user.authenticateUserInBackground(authenticationDetails, authenticationHandler);
如果注册由办公室完成并且用户获得其用户名和密码,那么您似乎需要从Cognito用户池中获取用户。要在Android App中验证用户,首先,您需要Cognito User Pool中的以下配置:
然后,您应该使用CognitoUserPool
在应用程序中创建用户池的实例,如下所示:
userPool = new CognitoUserPool(context, this.poolID, this.clientID, this.clientSecret, this.awsRegion);
要允许用户登录,请执行以下操作:
public void getUser(){
CognitoUser cognitoUser = userPool.getUser(userId);
cognitoUser.getSessionInBackground(authenticationHandler);
}
AuthenticationHandler authenticationHandler = new AuthenticationHandler() {
@Override
public void authenticationChallenge(ChallengeContinuation continuation) {
// Do Something
}
@Override
public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) {
Toast.makeText(appContext,"Sign in success", Toast.LENGTH_LONG).show();
// Do Something
}
@Override
public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) {
// The API needs user sign-in credentials to continue
AuthenticationDetails authenticationDetails = new AuthenticationDetails(userId, userPassword, null);
// Pass the user sign-in credentials to the continuation
authenticationContinuation.setAuthenticationDetails(authenticationDetails);
// Allow the sign-in to continue
authenticationContinuation.continueTask();
}
@Override
public void getMFACode(MultiFactorAuthenticationContinuation multiFactorAuthenticationContinuation) {
// Do Something
}
@Override
public void onFailure(Exception exception) {
// Do Something
}
};