在AWS Cognito中,当使用社交登录(如Facebook、Google等)时,可能会遇到"NotAuthorizedException: No provider named 'cognito-idp.{region}.amazonaws.com/{userPoolId}' found."的错误。这个错误通常是由于在AWS Cognito用户池中未配置正确的身份提供者引起的。
以下是一种解决方法,该方法包含React Native的代码示例:
首先,确保你已经在AWS Cognito用户池中正确地配置了身份提供者。在AWS管理控制台中,导航到Cognito服务,然后选择你的用户池。在用户池的左侧导航窗格中,选择"社交登录"选项卡。在这里,你应该添加你想要使用的身份提供者(如Facebook或Google),并配置相应的应用程序密钥和应用程序ID。
接下来,确保你的React Native应用程序正确设置了AWS Cognito。你可以使用AWS Amplify库来简化这个过程。首先,安装AWS Amplify库:
npm install aws-amplify --save
npm install aws-amplify-react-native --save
然后,在你的React Native应用程序的入口文件中,导入和配置AWS Amplify:
import Amplify from 'aws-amplify';
import awsconfig from './aws-exports';
Amplify.configure(awsconfig);
在上面的代码中,aws-exports.js是一个包含你的AWS Cognito配置的文件。你可以在AWS管理控制台中的Cognito服务中找到这些配置,以及其他必要的配置信息。
最后,当你在React Native应用程序中使用AWS Cognito进行社交登录时,确保在使用Auth模块之前先导入配置:
import Amplify, { Auth } from 'aws-amplify';
import awsconfig from './aws-exports';
Amplify.configure(awsconfig);
// 在进行社交登录之前,确保先进行用户登录
Auth.signIn(username, password)
.then(user => {
// 用户登录成功后,进行社交登录
Auth.federatedSignIn({ provider: 'Facebook' })
.then(credentials => {
// 社交登录成功
console.log(credentials);
})
.catch(error => {
// 社交登录失败
console.log(error);
});
})
.catch(error => {
// 用户登录失败
console.log(error);
});
在上面的代码中,首先使用Auth.signIn函数进行用户登录,然后使用Auth.federatedSignIn函数进行社交登录。在进行社交登录之前,确保先进行用户登录是必要的。
通过按照上述步骤配置AWS Cognito并在React Native应用程序中正确使用AWS Amplify,你应该能够解决"NotAuthorizedException: No provider named 'cognito-idp.{region}.amazonaws.com/{userPoolId}' found."的问题。
上一篇:Aws Cognito社交登录
下一篇:AWS Cognito身份池