gpt4 book ai didi

amazon-web-services - AWS Cognito/Amplify - 将新用户注册自动添加到用户组

转载 作者:行者123 更新时间:2023-12-04 04:56:36 39 4
gpt4 key购买 nike

我正在使用 AWS Amplify 库为 AppSync 项目注册和执行身份验证。这使用 Cognito。但是,当新用户通过 Amplify/Cognito 注册时,新用户不会被分配到认知池中的任何特定组。我正在使用 Amplify 高阶组件进行登录/注册。

import { withAuthenticator } from 'aws-amplify-react';

我包裹了一个组件
class Authenticator extends React.Component {
//... basically empty component, only exists so I can wrap it w/ the HOC
}
export default withAuthenticator(Authenticator)

Amplify 在 index.js 中设置
import config from './aws-exports';
import Amplify from 'aws-amplify';
Amplify.configure(config);

aws-exports.js 由 AWS Mobile Hub CLI 自动生成。好像...
const awsmobile = {
'aws_app_analytics': 'enable',
'aws_cognito_identity_pool_id': 'us-west-2:XXX',
'aws_cognito_region': 'us-west-2',
'aws_content_delivery': 'enable',
'aws_content_delivery_bucket': 'flashcards-hosting-mobilehub-XXX',
'aws_content_delivery_bucket_region': 'us-west-2',
'aws_content_delivery_cloudfront': 'enable',
'aws_content_delivery_cloudfront_domain': 'XXX.cloudfront.net',
'aws_mandatory_sign_in': 'enable',
'aws_mobile_analytics_app_id': 'XXX',
'aws_mobile_analytics_app_region': 'us-east-1',
'aws_project_id': 'XXX',
'aws_project_name': 'flash-cards',
'aws_project_region': 'us-west-2',
'aws_resource_name_prefix': 'flashcards-mobilehub-XXX',
'aws_sign_in_enabled': 'enable',
'aws_user_pools': 'enable',
'aws_user_pools_id': 'us-west-2_XXX',
'aws_user_pools_mfa_type': 'OFF',
'aws_user_pools_web_client_id': 'XXX',
}
export default awsmobile;

最佳答案

我让它工作了。正如 Vladamir 在评论中提到的,这需要在服务器端完成,在 Post Confirmation lambda 触发器中。这是 lambda 函数。

'use strict';
var AWS = require('aws-sdk');
module.exports.addUserToGroup = (event, context, callback) => {
// console.log("howdy!",event);
var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
var params = {
GroupName: 'users', //The name of the group in you cognito user pool that you want to add the user to
UserPoolId: event.userPoolId,
Username: event.userName
};
//some minimal checks to make sure the user was properly confirmed
if(! (event.request.userAttributes["cognito:user_status"]==="CONFIRMED" && event.request.userAttributes.email_verified==="true") )
callback("User was not properly confirmed and/or email not verified")
cognitoidentityserviceprovider.adminAddUserToGroup(params, function(err, data) {
if (err) {
callback(err) // an error occurred
}
callback(null, event); // successful response
});
};

您还必须为 lambda 函数角色设置策略。在 IAM 控制台中,找到此 lambda 的角色并添加此内联策略。这使 lambda 成为所有认知的城堡的 key ,因此使您的限制更加严格。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cognito-identity:*"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"cognito-sync:*"
],
"Resource": "*"
},
{ //this might be the only one you really need
"Effect": "Allow",
"Action": [
"cognito-idp:*"
],
"Resource": "*"
}
]
}

关于amazon-web-services - AWS Cognito/Amplify - 将新用户注册自动添加到用户组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49984024/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com