AWS Cognito Sign-In (Android)

后端 未结 3 1741
一整个雨季
一整个雨季 2021-01-06 19:25

I am trying to figure out how to sign in a User with AWS Cognito. The tutorials all seem to deal with Users from a standpoint of signing up Users, not signing them in. I do

3条回答
  •  一生所求
    2021-01-06 20:09

    If the registration is done by the office and user get their username and password, so it seems you need to get the users from Cognito User Pool. For authenticating the user in Android App, first, you will need the following configuration from Cognito User Pool:

    • Pool Id
    • App client id
    • App client secret
    • AWS Region

    Then you should create an instance of the user pool in your app by using CognitoUserPool, like follow:

    userPool = new CognitoUserPool(context, this.poolID, this.clientID, this.clientSecret, this.awsRegion);
    

    For allowing the user to sign-in, do the following:

    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
        }
    };
    

    You can find more information about integrating user sign-in and sign-up here and here.

提交回复
热议问题