在AWS Cognito控制台的“应用集成”下,选择“域名”。在“标识提供商”中添加Google并设置正确的凭据和授权链接。在“应用集成”中选择“域名”选项卡,将“授权范围”设置为openid和email。
在Google Cloud Console中,进入API和服务->凭据。选择Google OAuth2凭据,并将授权重定向URI添加为Cognito授权域。
在AWS Cognito控制台的“应用集成”下,选择“域名”。在“允许来源”中添加Cognito授权域。
在前端路由中,添加正确的域名,包括http/https,以反映Cognito授权域名。
示例代码:
const auth = new AWS.CognitoIdentityServiceProvider({
region: ‘REGION’,
userPoolId: ‘USER_POOL_ID’,
clientId: ‘CLIENT_ID’,
identityPoolId: ‘IDENTITY_POOL_ID’,
endpoint: ‘COGNITO_ENDPOINT’,
});
// sets the correct Cognito domain for your user pool
auth.userPoolDomain = ‘USER_POOL_DOMAIN’;
// sets the correct Google Client ID for your OAuth2 provider
auth.googleClientId = ‘GOOGLE_CLIENT_ID’;
// sets the correct redirect URI for your Google OAuth2 provider
auth.redirectUri = ‘REDIRECT_URI’;
// API Gateway endpoint for your backend
auth.apiGatewayEndpoint = ‘API_GATEWAY_ENDPOINT’;
// CORS configuration for the API Gateway endpoint
auth.xhrFields = { withCredentials: true };
// adds the Cognito domain for the CORS whitelist
auth.allowedOrigins = [‘USER_POOL_DOMAIN’];
// adds a listener for the Google OAuth2 token exchange
$(window).on(‘message’, function (event) {
if (event.original