AWSCognito提供的UI出现Google登录问题。
创始人
2024-09-24 14:00:13
0

根据AWS Cognito的文档,要想使用第三方身份验证提供程序,需要在AWS Cognito中添加相应的“身份提供者”(Identity Provider),包括Google。同时,需要将相应的OAuth 2.0客户端ID和客户机密添加到Google身份提供者设置中。

例如,以下是使用AWS Amplify和React Native创建Google登录的示例代码。

首先,安装必要的依赖项:

npm install aws-amplify @aws-amplify/ui-react react-native-google-signin react-native-linking

接下来,将Google身份提供者添加到AWS Cognito:

import Amplify, { Auth } from 'aws-amplify';

Amplify.configure({
  Auth: {
    identityPoolId: 'YOUR_IDENTITY_POOL_ID',
    region: 'YOUR_REGION',
    userPoolId: 'YOUR_USERPOOL_ID',
    userPoolWebClientId: 'YOUR_APP_CLIENT_ID',
    oauth: {
      domain: 'YOUR_AUTH_DOMAIN',
      scope: ['phone', 'email', 'profile', 'openid', 'aws.cognito.signin.user.admin'],
      redirectSignIn: 'com.yourapp://callback/',
      redirectSignOut: 'com.yourapp://signout/',
      responseType: 'code',
      options: {
        AdvancedSecurityDataCollectionFlag: true
      }
    },
    identityProviders: {
      Google: {}
    }
  }
});

这里的“YOUR_IDENTITY_POOL_ID”、“YOUR_REGION”、“YOUR_USERPOOL_ID”和“YOUR_APP_CLIENT_ID”可以在AWS Cognito控制台中找到。

接下来,使用React Native和Google登录按钮创建UI:

import React from 'react';
import { View, Button } from 'react-native';
import { GoogleSigninButton } from 'react-native-google-signin';
import { Auth } from 'aws-amplify';
import { onSignIn } from './auth';

export default class LoginScreen extends React.Component {
  async googleSignIn() {
    try {
      const user = await GoogleSignin.signIn();
      const accessToken = user.accessToken;
      const { idToken, email } = user;

      const credentials = await Auth.federatedSignIn(
        'google',
        { token: idToken, access_token: accessToken },
        { email }
      );

      onSignIn(credentials);

      this.props.navigation.navigate('App');
    } catch (error) {
      console.log(error);
    }
  }

  render() {
    return (
      
         this.googleSignIn()}
          disabled={this.state.isSigninInProgress}
        />
      
    );
  }
}

这里,“onSignIn”函数是用来将凭据存储在本地的,以便在用户下次打开应用程序时自动登录。另外,为了使应用程序能够正确地处理Google登录后的回调,需要在应用程序的链接中包含一个自定义方案(例如“com.yourapp”)。

最后,确保应用程序的Android清单文件中包含以下代码:


这些都是使Google登录与AWS Cognito的UI一起工作所需的步骤。

相关内容

热门资讯

据权威媒体报道!来玩德州破解器... 据权威媒体报道!来玩德州破解器,佛手大菠萝辅助,技法教程(揭秘有挂)1、在佛手大菠萝辅助插件功能辅助...
2026版总结!wepoker... 2026版总结!wepoker辅助器安装包定制,hhpoker到底可以作弊吗,操作教程(有挂解密)1...
第三方技巧!大菠萝辅助器,hh... 第三方技巧!大菠萝辅助器,hhpoker德州作弊,方式教程(有挂秘笈)1、hhpoker德州作弊公共...
近期!佛手大菠萝辅助,智星菠萝... 近期!佛手大菠萝辅助,智星菠萝有挂吗,步骤教程(真的有挂)1)佛手大菠萝辅助有没有挂:进一步探索佛手...
为切实保障!wepoker怎么... 您好,wepoker怎么下载游戏这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
长期以来!wepoker怎么发... 长期以来!wepoker怎么发冤家牌,hhpoker是正品吗,模板教程(有挂秘诀)wepoker怎么...
截至发稿!wepoker透视底... 截至发稿!wepoker透视底牌,wepoker究竟有没有透视,法子教程(揭秘有挂)1、wepoke...
近期!hhpoker真的有透视... 近期!hhpoker真的有透视吗,pokemmo手机版修改器,指南教程(证实有挂)1、不需要AI权限...
黑科技代打!wepoker私人... 黑科技代打!wepoker私人局辅助,竞技联盟辅助,操作教程(有挂方略)1、玩家可以在wepoker...
黑科技辅助挂!hhpoker透... 您好,德州来玩辅助器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家...