1

我正在尝试使用 Cognito 在我的 Flutter 应用程序中实现身份验证。我正在针对过去一年在我的 React 应用程序中成功使用的现有 userPool 进行身份验证。

但是,使用 Flutter 我无法获取用户的会话。我能够成功登录,但无法使用该fetchAuthSession()方法获取任何令牌。知道为什么会这样吗?这是我的一些工作和非工作代码:

此代码成功...

Future _usersEmail() async {
    try {
      var attributes = (await Amplify.Auth.fetchUserAttributes()).toList();
      for (var attribute in attributes) {
        if (attribute.userAttributeKey == 'email') {
          print("user's email is ${attribute.value}");
          return '${attribute.value}';
        }
      }
      return 'no email';
    } on AuthException catch (e) {
      return '${e.message}';
    }
  }

这段代码也成功了...

  Future<bool> _isSignedIn() async {
    final CognitoAuthSession session =
        await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;
    print('_isSignedIn: ${session.isSignedIn}');
    return session.isSignedIn;
  }

此代码返回空...

  Future _getIdToken() async {
    final CognitoAuthSession session =
        await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;
    final idToken = session.userPoolTokens?.idToken;
    print('idToken: $idToken');
    return idToken;
  }

这是我的放大配置...

{
  "UserAgent": "aws-amplify-cli/2.0",
  "Version": "1.0",
  "auth": {
      "plugins": {
          "awsCognitoAuthPlugin": {
              "UserAgent": "aws-amplify-cli/0.1.0",
              "Version": "0.1.0",
              "IdentityManager": {
                  "Default": {}
              },
              "CredentialsProvider": {
                  "CognitoIdentity": {
                      "Default": {
                          "PoolId": "us-east-1_abcxyz",
                          "Region": "us-east-1"
                      }
                  }
              },
              "CognitoUserPool": {
                  "Default": {
                      "PoolId": "us-east-1_abcxyz",
                      "AppClientId": "5j0kii90dJ09est43xh3X21",
                      "Region": "us-east-1"
                  }
              },
              "Auth": {
                  "Default": {
                      "authenticationFlowType": "USER_SRP_AUTH"
                  }
              }
          }
      }
  }
}
4

1 回答 1

0

您可能需要像这样在参数中设置getAWSCredentials为:trueoptions

   final authSession = (await Amplify.Auth.fetchAuthSession(
      options: CognitoSessionOptions(getAWSCredentials: true),
    )) as CognitoAuthSession;
于 2021-11-10T20:23:59.310 回答