【问题标题】:How to sign out of aws cognito - android?如何退出 aws cognito - android?
【发布时间】:2017-05-25 18:03:58
【问题描述】:

所以这是我用来让我的用户登录到 cognito 的代码(我希望我是正确的)。现在,我将如何退出?目前我有自己的注册流程(所以还没有 facebook 或 google)。

 // Callback handler for the sign-in process
    private AuthenticationHandler authenticationHandler = new AuthenticationHandler()
    {
        @Override
        public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice)
        {
            Log.d(COGNITO_LOGIN,"Login success!");
            cognitoUser.getDetailsInBackground(getDetailsHandler);
            //Now we get user from dynamoDB and store it into a local user object. 
        }
        @Override
        public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId)
        {
            Log.d(COGNITO_LOGIN,passwordET.getText().toString());
            // The API needs user sign-in credentials to continue
            AuthenticationDetails authenticationDetails = new AuthenticationDetails(userId, passwordET.getText().toString(), 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) {
            // Multi-factor authentication is required; get the verification code from user
            multiFactorAuthenticationContinuation.setMfaCode("verificationCode");
            // Allow the sign-in process to continue
            multiFactorAuthenticationContinuation.continueTask();
        }
        @Override
        public void authenticationChallenge(ChallengeContinuation continuation) {
        }
        @Override
        public void onFailure(Exception exception)
        {
            // Sign-in failed, check exception for the cause
            Log.d(COGNITO_LOGIN,"Login failed!");
            Log.d(COGNITO_LOGIN,exception.getMessage());
        }
    };
 cognitoUser.getSessionInBackground(authenticationHandler);

【问题讨论】:

    标签: android amazon-web-services amazon-s3 aws-sdk amazon-cognito


    【解决方案1】:

    您应该能够在 cognitoUser 对象上调用 signOut,如下所示。这样做是从设备清除访问、ID 和刷新令牌,因此您需要再次进行身份验证。

    // This has cleared all tokens and this user will have to go through the authentication process to get tokens.
    user.signOut();
    

    还有一个 globalSignOut 调用可以在服务器端撤销令牌。

    【讨论】:

    • 您知道如何检查用户当前是否登录吗?例如决定显示登录或注销按钮?
    • IdentityManager.getDefaultIdentityManager().isUserSignedIn();
    【解决方案2】:

    有一种方法可以擦除或清除当前登录用户的会话,以下是我目前找到的方法。

    This is for fb in federated identities 
      if (fbAccessToken != null) {
                                        LoginManager.getInstance().logOut();
                                    }
    This is for twiiter 
                                    if (mAuthManager != null) {
                                          mAuthManager.clearAuthorizationState(null);
                                    }
    
                                    // wipe data
                                    CognitoSyncClientManager.getInstance()
                                            .wipeData();
    

    【讨论】:

      【解决方案3】:
      CognitoUserPool pool = AwsCognitoHelper.getPool();
      if (pool != null) {
          CognitoUser user = pool.getCurrentUser();
          if (user != null) {
              GenericHandler handler = new GenericHandler() {
      
                  @Override
                  public void onSuccess() {
                  }
      
                  @Override
                  public void onFailure(Exception e) {
                  }
              };
              user.globalSignOutInBackground(handler);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2023-02-21
        • 2019-04-06
        • 1970-01-01
        • 2021-09-23
        • 2017-05-27
        • 1970-01-01
        • 1970-01-01
        • 2017-04-08
        • 2016-08-12
        相关资源
        最近更新 更多