您可以通过在Cognito控制台中配置域名,然后将其添加到您的AWS Cognito用户池中,以解决此问题。您还需要修改客户端设置以允许来自该域的跨域资源共享。以下是一些示例代码,演示如何在JavaScript应用程序中进行设置:
AWSCognito.config.region = 'YOUR_REGION';
AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'YOUR_IDENTITY_POOL_ID',
});
var poolData = {
UserPoolId: 'YOUR_USER_POOL_ID',
ClientId: 'YOUR_APP_CLIENT_ID',
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var cognitoUser = userPool.getCurrentUser();
if (cognitoUser != null) {
cognitoUser.getSession(function(err, session) {
if (err) {
// Handle error
} else {
// User is logged in
}
});
} else {
var authData = {
RedirectUriSignIn: 'http://localhost:3000', // The page to redirect to after successful login
ResponseType: 'token',
};
var auth = new AWSCognito.CognitoIdentityServiceProvider.Auth({
UserPoolId: 'YOUR_USER_POOL_ID',
ClientId: 'YOUR_APP_CLIENT_ID',
});
auth.showSignIn(authData, function(err, result) {
if (err) {
// Handle error
}
});
}
上述代码将authData对象中的RedirectUriSignIn属性设置为本地主机的URL,并将ResponseType属性设置为“token”。如果您按照此代码设置并仍然遇到CORS错误,请确保已按照上面提到的方式在您的用户池中配置了域名。