我正在使用amazon_cognito_2处理我的颤振应用程序中的身份验证

我正在使用AmazonuCognitouIdentityuDartu2处理我的Flatter应用程序中的身份验证。我通过电子邮件完成了身份验证。现在我还想完成Google身份验证,我正在遵循软件包文档中的用例19,但我遇到了一个错误。它没有加载谷歌的登录页面。我添加了cognitouPooluURL(us-east-1u XXXXXXXX)和**cognitouClientuID**(xxxxxxxxxxxxxxxxxxxxxx),它在URL中仍然不能正常工作。我错过了什么

附件:我正在使用geTx进行状态管理

它在移动设备上显示的内容:

运行URL时在web上显示的内容:

如何调用函数:

                              GestureDetector(
                                onTap: () async {
                                  Get.to(
                                    () => Scaffold(
                                      appBar: AppBar(),
                                      body: controller.getWebView(),
                                    ),
                                  );
                                },

文件:

用例19。将此库与cognito在移动设备上的联合登录一起使用。使用flatteruwebview()导航到cognito的授权URL。使用flatteruWebView的navigationdelegate捕获myapp://?代码重定向=。向cognito的令牌URL发出post请求以获取您的令牌。使用令牌创建会话和用户

final Completer _webViewController = Completer();
  Widget getWebView() {
    var url = "https://${COGNITO_POOL_URL}" +
      ".amazoncognito.com/oauth2/authorize?identity_provider=Google&redirect_uri=" +
      "myapp://&response_type=CODE&client_id=${COGNITO_CLIENT_ID}" +
      "&scope=email%20openid%20profile%20aws.cognito.signin.user.admin";
    return
      WebView(
        initialUrl: url,
        userAgent: 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) ' +
            'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Mobile Safari/537.36',
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController webViewController) {
          _webViewController.complete(webViewController);
        },
        navigationDelegate: (NavigationRequest request) {
          if (request.url.startsWith("myapp://?code=")) {
            String code = request.url.substring("myapp://?code=".length);
            signUserInWithAuthCode(code);
            return NavigationDecision.prevent;
          }
          return NavigationDecision.navigate;
        },
        gestureNavigationEnabled: true,
      );
  }
  final userPool = CognitoUserPool(
    'ap-southeast-1_xxxxxxxxx',
    'xxxxxxxxxxxxxxxxxxxxxxxxxx'
  );
  static Future signUserInWithAuthCode(String authCode) async {
    String url = "https://${COGNITO_POOL_URL}" +
        ".amazoncognito.com/oauth2/token?grant_type=authorization_code&client_id=" +
        "${COGNITO_CLIENT_ID}&code=" + authCode + "&redirect_uri=myapp://";
    final response = await http.post(url, body: {}, headers: {'Content-Type': 'application/x-www-form-urlencoded'});
    if (response.statusCode != 200) {
      throw Exception("Received bad status code from Cognito for auth code:" +
          response.statusCode.toString() + "; body: " + response.body);
    }
    final tokenData = json.decode(response.body);
    final idToken = CognitoIdToken(tokenData['id_token']);
    final accessToken = CognitoAccessToken(tokenData['access_token']);
    final refreshToken = CognitoRefreshToken(tokenData['refresh_token']);
    final session = CognitoUserSession(idToken, accessToken, refreshToken: refreshToken);
    final user = CognitoUser(null, userPool, signInUserSession: session);
    // NOTE: in order to get the email from the list of user attributes, make sure you select email in the list of
    // attributes in Cognito and map it to the email field in the identity provider.
    final attributes = await user.getUserAttributes();
    for (CognitoUserAttribute attribute in attributes) {
      if (attribute.getName() == "email") {
        user.username = attribute.getValue();
        break;
      }
    }
    return user;
  }
}

附件:我正在使用相同的代码填充请求的动态var-s

© 版权声明
THE END
喜欢就支持一下吧
点赞160赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容